Ejemplo n.º 1
0
        private void init(ConstructorInfo constructor)
        {
            if (constructor == null)
            {
                throw new ArgumentException("Argument: constructor is null");
            }
            if (!constructor.IsPublic)
            {
                var types = ReflectionHelper.ConvertToType(constructor.GetParameters());
                Constructor = arguments => constructor.Invoke(ReflectionHelper.GetArgumentByType(arguments, types));
                return;
            }

            DynamicMethod method = new DynamicMethod(ReflectionHelper.GetMemberSignName(constructor), ReflectionHelper.ObjectType, new Type[] { ReflectionHelper.ObjArrayType });

            var il   = method.GetILGenerator();
            var args = constructor.GetParameters();

            if (args != null)
            {
                for (byte i = 0; i < args.Length; i++)
                {
                    ReflectionHelper.ILLdarg(il, 0);
                    ReflectionHelper.ILLdelem(il, i, ReflectionHelper.ObjectType);
                    ReflectionHelper.ILCastclass(il, ReflectionHelper.ObjectType, args[i].ParameterType);
                }
            }
            il.Emit(OpCodes.Newobj, constructor);
            ReflectionHelper.ILCastclass(il, constructor.DeclaringType, ReflectionHelper.ObjectType);
            il.Emit(OpCodes.Ret);

            Constructor = (Func <object[], object>)method.CreateDelegate(typeof(Func <object[], object>));
        }
Ejemplo n.º 2
0
        void init(FieldInfo field)
        {
            if (field == null)
            {
                throw new ArgumentException("Argument: field is null");
            }

            var name = ReflectionHelper.GetMemberSignName(field);
            var type = field.DeclaringType;

#if DEBUG1
            ModuleBuilder         moduleBuilder = ReflectionHelp.EmitDebug();
            ISymbolDocumentWriter doc           = moduleBuilder.DefineDocument(@"d:\IL的cs源代码片断在这里.txt", Guid.Empty, Guid.Empty, Guid.Empty); //要定义源代码位置,这个文档不需要全部源代码,只需要你想调试的il源代码翻译就可以了

            DynamicMethod method = new DynamicMethod(name + "_get", ReflectionHelp.ObjectType, new Type[] { ReflectionHelp.ObjectType }, moduleBuilder);
#else
            DynamicMethod method = new DynamicMethod(name + "_get", ReflectionHelper.ObjectType, new Type[] { ReflectionHelper.ObjectType });
#endif

            var il = method.GetILGenerator();
            ReflectionHelper.ILLdarg(il, 0);
            ReflectionHelper.ILCastclass(il, ReflectionHelper.ObjectType, type);
            il.Emit(OpCodes.Ldfld, field);
            ReflectionHelper.ILCastclass(il, field.FieldType, ReflectionHelper.ObjectType);

#if DEBUG1
            il.MarkSequencePoint(doc, 1, 0, 1, 100);//DynamicMethod不支持
#endif

            il.Emit(OpCodes.Ret);
            FieldGet = (Func <object, object>)method.CreateDelegate(typeof(Func <object, object>));

            method = new DynamicMethod(name + "_set", ReflectionHelper.VoidType, new Type[] { ReflectionHelper.ObjectType, ReflectionHelper.ObjectType });
            il     = method.GetILGenerator();
            ReflectionHelper.ILLdarg(il, 0);
            ReflectionHelper.ILCastclass(il, ReflectionHelper.ObjectType, type);
            ReflectionHelper.ILLdarg(il, 1);
            ReflectionHelper.ILCastclass(il, ReflectionHelper.ObjectType, field.FieldType);
            il.Emit(OpCodes.Stfld, field);
            il.Emit(OpCodes.Ret);
            FieldSet = (Action <object, object>)method.CreateDelegate(typeof(Action <object, object>));
        }
Ejemplo n.º 3
0
        void init(MethodInfo method)
        {
            if (method == null)
            {
                throw new ArgumentException("Argument: method is null");
            }

            var           type    = method.DeclaringType;
            DynamicMethod Dmethod = new DynamicMethod(ReflectionHelper.GetMemberSignName(method), ReflectionHelper.ObjectType, new Type[] { ReflectionHelper.ObjectType, ReflectionHelper.ObjArrayType });

            var il = Dmethod.GetILGenerator();

            ReflectionHelper.ILLdarg(il, 0);
            ReflectionHelper.ILCastclass(il, ReflectionHelper.ObjectType, type);
            var args = method.GetParameters();

            if (args != null)
            {
                for (byte i = 0; i < args.Length; i++)
                {
                    ReflectionHelper.ILLdarg(il, 1);
                    ReflectionHelper.ILLdelem(il, i, ReflectionHelper.ObjectType);
                    ReflectionHelper.ILCastclass(il, ReflectionHelper.ObjectType, args[i].ParameterType);
                }
            }
            il.Emit(OpCodes.Callvirt, method);
            if (method.ReturnType != ReflectionHelper.VoidType)
            {
                ReflectionHelper.ILCastclass(il, method.ReturnType, ReflectionHelper.ObjectType);
            }
            else
            {
                il.Emit(OpCodes.Ldnull);
            }
            il.Emit(OpCodes.Ret);
            MethodInvoker = (Func <object, object[], object>)Dmethod.CreateDelegate(typeof(Func <object, object[], object>));
        }
Ejemplo n.º 4
0
        void init(PropertyInfo property)
        {
            if (property == null)
            {
                throw new ArgumentException("Argument: property is null");
            }
            var getmethod = property.GetGetMethod();
            var setmethod = property.GetSetMethod();

            //PropertyGet = new DelegateMethodInvoker(property.GetGetMethod());
            //PropertySet = new DelegateMethodInvoker(property.GetSetMethod());

            var name = ReflectionHelper.GetMemberSignName(property);
            var type = property.DeclaringType;

            if (getmethod != null && !getmethod.IsPublic)
            {
                PropertyGet = obj => property.GetValue(obj, null);
            }
            else
            {
                DynamicMethod method = new DynamicMethod(name + "_get", ReflectionHelper.ObjectType, new Type[] { ReflectionHelper.ObjectType });
                var           il     = method.GetILGenerator();
                if (getmethod != null)
                {
                    ReflectionHelper.ILLdarg(il, 0);
                    ReflectionHelper.ILCastclass(il, ReflectionHelper.ObjectType, type);
                    il.Emit(OpCodes.Callvirt, getmethod);
                    ReflectionHelper.ILCastclass(il, property.PropertyType, ReflectionHelper.ObjectType);
                    il.Emit(OpCodes.Ret);
                }
                else
                {
                    ReflectionHelper.ILThrow <MethodAccessException>(il, "get method not found");
                }
                PropertyGet = (Func <object, object>)method.CreateDelegate(typeof(Func <object, object>));
            }
            if (setmethod != null && !setmethod.IsPublic)
            {
                PropertySet = (obj, val) => property.SetValue(obj, val, null);
            }
            else
            {
                DynamicMethod method = new DynamicMethod(name + "_set", ReflectionHelper.VoidType, new Type[] { ReflectionHelper.ObjectType, ReflectionHelper.ObjectType });
                var           il     = method.GetILGenerator();
                if (setmethod != null)
                {
                    ReflectionHelper.ILLdarg(il, 0);
                    ReflectionHelper.ILCastclass(il, ReflectionHelper.ObjectType, type);
                    ReflectionHelper.ILLdarg(il, 1);
                    ReflectionHelper.ILCastclass(il, ReflectionHelper.ObjectType, property.PropertyType);
                    il.Emit(OpCodes.Callvirt, setmethod);
                    il.Emit(OpCodes.Ret);
                }
                else
                {
                    ReflectionHelper.ILThrow <MethodAccessException>(il, "set method not found");
                }

                PropertySet = (Action <object, object>)method.CreateDelegate(typeof(Action <object, object>));
            }
        }