c# - 方法实现中的主体和声明的签名不匹配

标签 c# .net-core cil reflection.emit

我想在运行时实现一个接口(interface),但是当我执行时

return Activator.CreateInstance(typeBuilder.CreateTypeInfo().AsType(), new RPCRequestProxy());

RPCClientFactory.cs 它抛出

System.TypeLoadException: Signature of the body and declaration in a method implementation do not match.



我使用 dotnet 核心 1.0.4。

入口点程序.cs :
namespace RPC
{
    interface IRCPClient
    {
        Task Foo([UrlParam]int b, [UrlParam]string a, [UrlParam] DateTime c);
    }

    class Program
    {
        static void Main(string[] args)
        {
            var factory = new RPCClientFactory();
            factory.Register<IRCPClient>();

            var cli = factory.GetClient<IRCPClient>();
            cli.Foo(1, "HelloWorld", DateTime.Now);

            Console.ReadKey();
        }
    }
}

RPCClientFactory.cs :
    public class RPCClientFactory
    {
        private readonly ConcurrentDictionary<Type, object> _clients = new ConcurrentDictionary<Type, object>();

        public void Register<ClientType>()
        {
            Register(typeof(ClientType));
        }

        public void Register(Type type)
        {
            if (!_clients.TryAdd(type, CreateImplInstance(type)))
                throw new InvalidOperationException($"bad type{type}");
        }

        public object CreateImplInstance(Type type)
        {
            var typeBuilder = CreateTypeBuilder(type);
            var methods = type.GetMethods();

            var field = CreateFiled(typeBuilder);
            CreateCotr(typeBuilder, field);
            CreateMethods(typeBuilder, methods, field);

            typeBuilder.AddInterfaceImplementation(type);

            return Activator.CreateInstance(typeBuilder.CreateTypeInfo().AsType(), new RPCRequestProxy());
        }

        private static TypeBuilder CreateTypeBuilder(Type type)
        {
            var typeSignature = "MyDynamicType";
            var an = new AssemblyName(typeSignature);
            var assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName(Guid.NewGuid().ToString()), AssemblyBuilderAccess.Run);
            ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("MainModule");
            TypeBuilder tb = moduleBuilder.DefineType(typeSignature,
                    TypeAttributes.Public |
                    TypeAttributes.Class |
                    TypeAttributes.AutoClass |
                    TypeAttributes.AnsiClass |
                    TypeAttributes.BeforeFieldInit |
                    TypeAttributes.AutoLayout,
                    null);

            return tb;
        }

        private static FieldInfo CreateFiled(TypeBuilder typeBuilder)
        {
            return typeBuilder.DefineField("_proxy", typeof(RPCRequestProxy), FieldAttributes.Private);
        }

        private static void CreateCotr(TypeBuilder typeBuilder, FieldInfo proxyField)
        {
            var ctorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public
                , CallingConventions.Standard
                , new[] { typeof(RPCRequestProxy) });

            var il = ctorBuilder.GetILGenerator();

            il.Emit(OpCodes.Ldarg_0);
            il.Emit(OpCodes.Call, typeof(object).GetConstructor(new Type[0]));
            il.Emit(OpCodes.Ldarg_0);
            il.Emit(OpCodes.Ldarg_1);
            il.Emit(OpCodes.Stfld, proxyField);
            il.Emit(OpCodes.Ret);
        }

        public void CreateMethods(TypeBuilder typeBuilder, MethodInfo[] methods, FieldInfo field)
        {
            foreach (var method in methods)
            {
                CreateMethod(typeBuilder, method, field);
            }
        }

        private static void CreateMethod(TypeBuilder typeBuilder, MethodInfo method, FieldInfo proxyFiled)
        {
            var paramters = method.GetParameters();

            var methodBuilder = typeBuilder.DefineMethod(method.Name,
                MethodAttributes.Public,
                CallingConventions.Standard,
                method.ReturnType,
                paramters.Select(x => x.GetType()).ToArray());

            var il = methodBuilder.GetILGenerator();

            il.Emit(OpCodes.Newobj, typeof(Dictionary<string, object>).GetConstructor(new Type[0]));
            il.Emit(OpCodes.Stloc_0);

            for (var i = 0; i < paramters.Length; ++i)
            {
                var param = paramters[i];

                if (param.GetCustomAttribute<BodyParamAttribute>() != null) continue;

                il.Emit(OpCodes.Ldloc_0);
                il.Emit(OpCodes.Ldstr, param.Name);
                il.Emit(OpCodes.Ldarg, i + 1);

                if (param.ParameterType.GetTypeInfo().IsValueType)
                {
                    il.Emit(OpCodes.Box, param.GetType());
                }

                il.Emit(OpCodes.Callvirt, typeof(Dictionary<string, object>).GetMethod("Add"));
            }

            var proxyMethod = typeof(RPCRequestProxy)
                .GetMethods()
                .First(x => x.Name == "PostAsync" && x.GetParameters().Length == 2);

            il.Emit(OpCodes.Ldarg_0);
            il.Emit(OpCodes.Ldfld, proxyFiled);
            il.Emit(OpCodes.Ldstr, "xxx.xxx.xxx");
            il.Emit(OpCodes.Ldloc_0);
            il.Emit(OpCodes.Callvirt, proxyMethod);
            il.Emit(OpCodes.Stloc_1);
            il.Emit(OpCodes.Ldloc_1);
            il.Emit(OpCodes.Ret);

            typeBuilder.DefineMethodOverride(methodBuilder, method);
        }

        public ClientType GetClient<ClientType>() where ClientType : class
        {
            _clients.TryGetValue(typeof(ClientType), out object cli);
            return cli as ClientType;
        }

        public ClientType GetRequiredClient<ClientType>() where ClientType : class
        {
            var cli = GetClient<ClientType>();

            if (cli == null) throw new InvalidOperationException($"bad type{typeof(ClientType)}");

            return cli;
        }
    }

RPCRequestProxy.cs :
    public class RPCRequestProxy
    {
        public RPCRequestProxy()
        {

        }

        public async Task PostAsync(string url, IDictionary<string, object> urlParams)
        {
            await Task.Run(() =>
            {
                Console.WriteLine("URL:"  + url);

                Console.WriteLine("QueryString:");
                foreach (var kv in urlParams)
                {
                    Console.WriteLine($"{kv.Key}={kv.Value}");
                }

            });
        }
    }

注册 IRCPClient 后,我​​希望它生成一个这样的类:
    class RCPClient : IRCPClient
    {
        private readonly RPCRequestProxy _proxy;

        public RCPClient(RPCRequestProxy proxy)
        {
            _proxy = proxy;
        }

        public Task Foo([UrlParam] int b, [UrlParam] string a, [UrlParam] DateTime c)
        {
            var urlParams = new Dictionary<string, object>();

            urlParams.Add(nameof(b), b);
            urlParams.Add(nameof(a), a);
            urlParams.Add(nameof(c), c);

            return _proxy.PostAsync("xxx.xxx.xxx", urlParams);
        }
    }

最佳答案

最后,我解决了:

1:我们必须声明本地 在我们使用本地值之前。

2:更改paramters.Select(x => x.GetType()).ToArray()paramters.Select(x => x.ParameterType).ToArray()
这是我的最终 创建方法 的方法| RPCClientFactory.cs

        private static void CreateMethod(TypeBuilder typeBuilder, MethodInfo method, FieldInfo proxyFiled)
        {
            var paramters = method.GetParameters();

            var methodBuilder = typeBuilder.DefineMethod(method.Name,
                MethodAttributes.Public 
                | MethodAttributes.Virtual
                | MethodAttributes.HideBySig
                | MethodAttributes.SpecialName,
                CallingConventions.Standard,
                method.ReturnType,
                paramters.Select(x => x.ParameterType).ToArray()); // change this line

            var il = methodBuilder.GetILGenerator();

            il.DeclareLocal(typeof(Dictionary<string, object>)); // add this line
            il.Emit(OpCodes.Newobj, typeof(Dictionary<string, object>).GetConstructor(new Type[0]));
            il.Emit(OpCodes.Stloc_0);

            for (short i = 0; i < paramters.Length; ++i)
            {
                var param = paramters[i];


                il.Emit(OpCodes.Ldloc_0);
                il.Emit(OpCodes.Ldstr, param.Name);
                il.Emit(OpCodes.Ldarg, (short)(i +1));

                if (param.ParameterType.GetTypeInfo().IsValueType)
                {
                    il.Emit(OpCodes.Box, param.ParameterType);
                }

                il.Emit(OpCodes.Callvirt, typeof(Dictionary<string, object>).GetMethod("Add"));
            }

            var proxyMethod = typeof(RPCRequestProxy)
                .GetMethods()
                .First(x => x.Name == "PostAsync" && x.GetParameters().Length == 2);

            il.Emit(OpCodes.Ldarg_0);
            il.Emit(OpCodes.Ldfld, proxyFiled);
            il.Emit(OpCodes.Ldstr, "xxx.xxx.xxx");
            il.Emit(OpCodes.Ldloc_0);
            il.Emit(OpCodes.Callvirt, proxyMethod);
            il.Emit(OpCodes.Ret);
        }

关于c# - 方法实现中的主体和声明的签名不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44993532/

相关文章:

c# - 提交 WP7 申请试用版

c# - 使用变量名称访问子对象

c# - 如何在C#.Net Core中创建多层应用程序的Nuget包?

c# - CI 构建定义中的新 .net core Visual Studio 2017 项目 nuget restore

c# - 从 Action 过滤器查看模型

c# - 通过 DynamicMethod 调用可变参数方法

c# - ldc.i4.x 和 ldc.i4 x 指令是否可以互换且语义相同?

.NET 4.5 MethodBuilder.SetMethodBody

c# - 数据类型 text 和 varchar 在等于运算符中不兼容

c# - 部署失败 : CLR Trigger