Example #1
0
        private SetPropertyValueDelegate CreateSetPropertyValueMethod(PropertyInfo pi)
        {
            MethodInfo methodInfo = pi.GetSetMethod(true);

            if (methodInfo == null)
            {
                return(null);
            }

            DynamicMethod method =
                ILUtil.CreateDynamicMethod(m_reflectedType, typeof(void), new Type[] { typeof(object), typeof(object) });

            ILGenerator il = method.GetILGenerator();

            LocalBuilder thisLocal = il.DeclareLocal(m_refinedType);

            il.Emit(OpCodes.Ldarg_0);
            ILUtil.EmitCastToReference(il, m_reflectedType);
            il.Emit(OpCodes.Stloc, thisLocal.LocalIndex);
            il.Emit(OpCodes.Ldloc, thisLocal);

            il.Emit(OpCodes.Ldarg_1);

            ILUtil.UnboxIfNeeded(il, methodInfo.GetParameters()[0].ParameterType);

            il.Emit(OpCodes.Call, methodInfo);

            il.Emit(OpCodes.Ret);

            return((SetPropertyValueDelegate)method.CreateDelegate(typeof(SetPropertyValueDelegate)));
        }
Example #2
0
        private SetFieldValueDelegate CreateSetFieldValueMethod(FieldInfo field)
        {
            DynamicMethod method =
                ILUtil.CreateDynamicMethod(m_reflectedType, null, new Type[] { typeof(object), typeof(object) });

            ILGenerator il = method.GetILGenerator();

            LocalBuilder thisLocal = il.DeclareLocal(m_refinedType);

            il.Emit(OpCodes.Ldarg_0);
            ILUtil.EmitCastToReference(il, m_reflectedType);
            il.Emit(OpCodes.Stloc, thisLocal.LocalIndex);

            Type valueType = field.FieldType;

            // load 'this'
            il.Emit(OpCodes.Ldloc, thisLocal);

            il.Emit(OpCodes.Ldarg_1);

            ILUtil.PrepareFieldForSet(il, valueType);

            il.Emit(OpCodes.Stfld, field);

            il.Emit(OpCodes.Ret);

            return((SetFieldValueDelegate)method.CreateDelegate(typeof(SetFieldValueDelegate)));
        }
Example #3
0
        private GetPropertyValueDelegate CreateGetPropertyValueMethod(PropertyInfo pi)
        {
            MethodInfo methodInfo = pi.GetGetMethod();

            if (methodInfo == null)
            {
                return(null);
            }

            DynamicMethod method =
                ILUtil.CreateDynamicMethod(m_reflectedType, typeof(object), new Type[] { typeof(object) });

            ILGenerator il = method.GetILGenerator();

            LocalBuilder thisLocal = il.DeclareLocal(m_refinedType);

            il.Emit(OpCodes.Ldarg_0);
            ILUtil.EmitCastToReference(il, m_reflectedType);
            il.Emit(OpCodes.Stloc, thisLocal.LocalIndex);
            il.Emit(OpCodes.Ldloc, thisLocal);

            il.EmitCall(OpCodes.Callvirt, methodInfo, null);

            ILUtil.BoxIfNeeded(il, pi.PropertyType);

            il.Emit(OpCodes.Ret);

            return((GetPropertyValueDelegate)method.CreateDelegate(typeof(GetPropertyValueDelegate)));
        }
Example #4
0
        private GetFieldValueDelegate CreateGetFieldValueMethod(FieldInfo field)
        {
            DynamicMethod method =
                ILUtil.CreateDynamicMethod(m_reflectedType, typeof(object), new Type[] { typeof(object) });

            ILGenerator il = method.GetILGenerator();

            LocalBuilder thisLocal = il.DeclareLocal(m_refinedType);
            LocalBuilder dataLocal = il.DeclareLocal(typeof(object));

            il.Emit(OpCodes.Ldarg_0);
            ILUtil.EmitCastToReference(il, m_reflectedType);
            il.Emit(OpCodes.Stloc, thisLocal);

            ConstructorInfo ci = ReflectionUtil.GetDefaultConstructor(typeof(object));

            il.Emit(OpCodes.Newobj, ci);
            il.Emit(OpCodes.Stloc, dataLocal);

//              get the value
            il.Emit(OpCodes.Ldloc, thisLocal);
            il.Emit(OpCodes.Ldfld, field);
            ILUtil.BoxIfNeeded(il, field.FieldType);

            il.Emit(OpCodes.Stloc, dataLocal);
            il.Emit(OpCodes.Ldloc, dataLocal);

            il.Emit(OpCodes.Ret);

            return((GetFieldValueDelegate)method.CreateDelegate(typeof(GetFieldValueDelegate)));
        }
Example #5
0
        private SetFieldValuesDelegate CreateSetFieldValuesMethod(FieldInfo[] fields)
        {
            Type[]        methodArguments = new Type[] { typeof(object), typeof(object[]) };
            DynamicMethod method          = ILUtil.CreateDynamicMethod(m_reflectedType, null, methodArguments);

            ILGenerator il = method.GetILGenerator();

            LocalBuilder thisLocal = il.DeclareLocal(m_refinedType);

            il.Emit(OpCodes.Ldarg_0);
            ILUtil.EmitCastToReference(il, m_reflectedType);
            il.Emit(OpCodes.Stloc, thisLocal.LocalIndex);

            for (int i = 0; i < fields.Length; i++)
            {
                Type valueType = fields[i].FieldType;

                // load 'this'
                il.Emit(OpCodes.Ldloc, thisLocal);

                il.Emit(OpCodes.Ldarg_1);
                il.Emit(OpCodes.Ldc_I4, i);
                il.Emit(OpCodes.Ldelem_Ref);

                ILUtil.PrepareFieldForSet(il, valueType);

                il.Emit(OpCodes.Stfld, fields[i]);
            }

            il.Emit(OpCodes.Ret);

            return((SetFieldValuesDelegate)method.CreateDelegate(typeof(SetFieldValuesDelegate)));
        }
Example #6
0
        private CreateInstanceDelegate CreateCreateInstanceMethod()
        {
            if (m_reflectedType.IsInterface || m_reflectedType.IsAbstract)
            {
                return(null);
            }

            DynamicMethod method = ILUtil.CreateDynamicMethod(m_reflectedType, typeof(object), null);

            ILGenerator il = method.GetILGenerator();

            if (m_reflectedType.IsValueType)
            {
                LocalBuilder tmpLocal = il.DeclareLocal(m_reflectedType);
                il.Emit(OpCodes.Ldloca, tmpLocal);
                il.Emit(OpCodes.Initobj, m_reflectedType);
                il.Emit(OpCodes.Ldloc, tmpLocal);
                il.Emit(OpCodes.Box, m_reflectedType);
            }
            else
            {
                ConstructorInfo constructor = ReflectionUtil.GetDefaultConstructor(m_reflectedType);

                if (constructor == null)
                {
                    return(null);
                }

                il.Emit(OpCodes.Newobj, constructor);
            }

            il.Emit(OpCodes.Ret);

            return((CreateInstanceDelegate)method.CreateDelegate(typeof(CreateInstanceDelegate)));
        }
Example #7
0
        public static void PrepareFieldForSet(ILGenerator il, Type propertyType)
        {
            if (propertyType.IsValueType)
            {
                Label        notNullLabel  = il.DefineLabel();
                Label        nullDoneLabel = il.DefineLabel();
                LocalBuilder localNew      = il.DeclareLocal(propertyType);

                il.Emit(OpCodes.Dup);
                il.Emit(OpCodes.Brtrue_S, notNullLabel);

                il.Emit(OpCodes.Pop);
                il.Emit(OpCodes.Ldloca, localNew);
                il.Emit(OpCodes.Initobj, propertyType);
                il.Emit(OpCodes.Ldloc, localNew);
                il.Emit(OpCodes.Br_S, nullDoneLabel);

                il.MarkLabel(notNullLabel);

                il.Emit(OpCodes.Unbox, propertyType);

                OpCode specificOpCode;

                if (ILUtil.GetOpCodeByType(propertyType, out specificOpCode))
                {
                    il.Emit(specificOpCode);
                }
                else
                {
                    il.Emit(OpCodes.Ldobj, propertyType);
                }

                il.MarkLabel(nullDoneLabel);
            }
            else
            {
                if (propertyType != typeof(object))
                {
                    il.Emit(OpCodes.Castclass, propertyType);
                }
            }
        }
Example #8
0
        private GetFieldValuesDelegate CreateGetFieldValuesMethod(FieldInfo[] fields)
        {
            Type[] methodArguments = new Type[] { typeof(object) };

            DynamicMethod method = ILUtil.CreateDynamicMethod(m_reflectedType, typeof(object[]), methodArguments);

            ILGenerator il = method.GetILGenerator();

            LocalBuilder thisLocal = il.DeclareLocal(m_refinedType);
            LocalBuilder dataLocal = il.DeclareLocal(typeof(object[]));

            il.Emit(OpCodes.Ldarg_0);
            ILUtil.EmitCastToReference(il, m_reflectedType);
            il.Emit(OpCodes.Stloc, thisLocal);

            il.Emit(OpCodes.Ldc_I4, fields.Length);
            il.Emit(OpCodes.Newarr, typeof(object));
            il.Emit(OpCodes.Stloc, dataLocal);

            for (int i = 0; i < fields.Length; i++)
            {
                FieldInfo field = fields[i];

                il.Emit(OpCodes.Ldloc, dataLocal);
                il.Emit(OpCodes.Ldc_I4, i);

                // get the value
                il.Emit(OpCodes.Ldloc, thisLocal);
                il.Emit(OpCodes.Ldfld, field);
                ILUtil.BoxIfNeeded(il, field.FieldType);

                il.Emit(OpCodes.Stelem_Ref);
            }

            il.Emit(OpCodes.Ldloc, dataLocal.LocalIndex);
            il.Emit(OpCodes.Ret);

            return((GetFieldValuesDelegate)method.CreateDelegate(typeof(GetFieldValuesDelegate)));
        }
Example #9
0
        public static InvokeMethodDelegate CreateMethodInvoker(MethodInfo methodInfo)
        {
            DynamicMethod method = ILUtil.CreateDynamicMethod(
                methodInfo.DeclaringType,
                typeof(object),
                new Type[] { typeof(object), typeof(object[]) });

            ILGenerator il = method.GetILGenerator();

            ParameterInfo[] parameters = methodInfo.GetParameters();

            Type[] paramTypes = new Type[parameters.Length];

            for (int i = 0; i < paramTypes.Length; i++)
            {
                if (parameters[i].ParameterType.IsByRef)
                {
                    paramTypes[i] = parameters[i].ParameterType.GetElementType();
                }
                else
                {
                    paramTypes[i] = parameters[i].ParameterType;
                }
            }

            LocalBuilder[] locals = new LocalBuilder[paramTypes.Length];

            for (int i = 0; i < paramTypes.Length; i++)
            {
                locals[i] = il.DeclareLocal(paramTypes[i], true);
            }

            for (int i = 0; i < paramTypes.Length; i++)
            {
                il.Emit(OpCodes.Ldarg_1);

                if (i >= -1 && i <= 8)
                {
                    il.Emit(ILUtil.GetOpCodeByInt(i));
                }
                else
                {
                    if (i > -129 && i < 128)
                    {
                        il.Emit(OpCodes.Ldc_I4_S, (SByte)i);
                    }
                    else
                    {
                        il.Emit(OpCodes.Ldc_I4, i);
                    }
                }

                il.Emit(OpCodes.Ldelem_Ref);

//                ILUtil.EmitCastToReference(il, paramTypes[i]);

                if (paramTypes[i].IsValueType)
                {
                    il.Emit(OpCodes.Unbox_Any, paramTypes[i]);
                }
                else
                {
                    il.Emit(OpCodes.Castclass, paramTypes[i]);
                }

                il.Emit(OpCodes.Stloc, locals[i]);
            }

            if (!methodInfo.IsStatic)
            {
                il.Emit(OpCodes.Ldarg_0);
            }

            for (int i = 0; i < paramTypes.Length; i++)
            {
                if (parameters[i].ParameterType.IsByRef)
                {
                    il.Emit(OpCodes.Ldloca_S, locals[i]);
                }
                else
                {
                    il.Emit(OpCodes.Ldloc, locals[i]);
                }
            }

            if (methodInfo.IsStatic)
            {
                il.EmitCall(OpCodes.Call, methodInfo, null);
            }
            else
            {
                il.EmitCall(OpCodes.Callvirt, methodInfo, null);
            }

            if (methodInfo.ReturnType == typeof(void))
            {
                il.Emit(OpCodes.Ldnull);
            }
            else
            {
                ILUtil.BoxIfNeeded(il, methodInfo.ReturnType);
            }

            for (int i = 0; i < paramTypes.Length; i++)
            {
                if (parameters[i].ParameterType.IsByRef)
                {
                    il.Emit(OpCodes.Ldarg_1);

                    if (i >= -1 && i <= 8)
                    {
                        il.Emit(ILUtil.GetOpCodeByInt(i));
                    }
                    else
                    {
                        if (i > -129 && i < 128)
                        {
                            il.Emit(OpCodes.Ldc_I4_S, (SByte)i);
                        }
                        else
                        {
                            il.Emit(OpCodes.Ldc_I4, i);
                        }
                    }

                    il.Emit(OpCodes.Ldloc, locals[i]);

                    if (locals[i].LocalType.IsValueType)
                    {
                        il.Emit(OpCodes.Box, locals[i].LocalType);
                    }

                    il.Emit(OpCodes.Stelem_Ref);
                }
            }

            il.Emit(OpCodes.Ret);

            return((InvokeMethodDelegate)method.CreateDelegate(typeof(InvokeMethodDelegate)));
        }