コード例 #1
0
        private static ReflectionEmitUtils.SetterMethod GenerateSetter(Type memberType, Type parentType, Action <ILGenerator> codeGenerator)
        {
            Type typeFromHandle = typeof(void);

            Type[] argumentTypes = new Type[]
            {
                typeof(object),
                typeof(object)
            };
            return(ReflectionEmitUtils.GenerateMethod <ReflectionEmitUtils.SetterMethod>(typeFromHandle, argumentTypes, parentType, delegate(ILGenerator il)
            {
                il.Emit(OpCodes.Ldarg_0);
                if (parentType.IsValueType)
                {
                    il.Emit(OpCodes.Unbox, parentType);
                }
                else
                {
                    il.Emit(OpCodes.Castclass, parentType);
                }
                il.Emit(OpCodes.Ldarg_1);
                il.Emit(OpCodes.Unbox_Any, memberType);
                codeGenerator(il);
            }));
        }
コード例 #2
0
        public static ReflectionEmitUtils.ConstructorMethod GenerateConstructor(Type type)
        {
            ConstructorInfo constructor = null;

            if (!type.IsValueType)
            {
                constructor = type.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, Type.EmptyTypes, null);
                if (constructor == null)
                {
                    return(null);
                }
            }
            Type typeFromHandle = typeof(object);

            Type[] emptyTypes = Type.EmptyTypes;
            return(ReflectionEmitUtils.GenerateMethod <ReflectionEmitUtils.ConstructorMethod>(typeFromHandle, emptyTypes, type, delegate(ILGenerator il)
            {
                if (type.IsValueType)
                {
                    il.DeclareLocal(type);
                    il.Emit(OpCodes.Ldloca_S, 0);
                    il.Emit(OpCodes.Initobj, type);
                    il.Emit(OpCodes.Ldloc_0);
                    il.Emit(OpCodes.Box, type);
                    return;
                }
                il.Emit(OpCodes.Newobj, constructor);
            }));
        }
コード例 #3
0
        public static ReflectionEmitUtils.SetterMethod GenerateAddToCollection(Type collectionType, Type elementType)
        {
            Type typeFromHandle = typeof(void);

            Type[] argumentTypes = new Type[]
            {
                typeof(object),
                typeof(object)
            };
            return(ReflectionEmitUtils.GenerateMethod <ReflectionEmitUtils.SetterMethod>(typeFromHandle, argumentTypes, elementType, delegate(ILGenerator il)
            {
                il.Emit(OpCodes.Ldarg_0);
                il.Emit(OpCodes.Castclass, collectionType);
                il.Emit(OpCodes.Ldarg_1);
                il.Emit(OpCodes.Unbox_Any, elementType);
                Type[] types = new Type[]
                {
                    elementType
                };
                MethodInfo method = collectionType.GetMethod("Add", types);
                il.Emit(OpCodes.Callvirt, method);
            }));
        }