Beispiel #1
0
        private static System.Action <TTarget, TValue> EmitPropertySetter <TTarget, TValue>(System.Reflection.PropertyInfo propertyInfo, System.Reflection.MethodInfo setMethod)
        {
            var propType      = propertyInfo.PropertyType;
            var declaringType = propertyInfo.DeclaringType;
            var dynamicMethod = EmitUtils.CreateDynamicMethod(string.Format("{0}_{1}", "$Set", propertyInfo.Name),
                                                              null,
                                                              new[] { typeof(TTarget), typeof(TValue) },
                                                              declaringType);
            var il = dynamicMethod.GetILGenerator();

            il.DeclareLocal(propType);
            il.Ldarg_1();
            if (!typeof(TValue).GetTypeInfo().IsValueType)
            {
                il.CastValue(propType);
            }
            il.Stloc_0();

            if (!setMethod.IsStatic)
            {
                il.Ldarg_0();
                il.CastReference(declaringType);
            }

            il.Ldloc_0();
            il.CallMethod(setMethod);
            il.Ret();

            return((System.Action <TTarget, TValue>)dynamicMethod.CreateDelegate(typeof(System.Action <TTarget, TValue>)));
        }
Beispiel #2
0
        private static System.Func <TSource, TReturn> EmitPropertyGetter <TSource, TReturn>(System.Reflection.PropertyInfo propertyInfo, System.Reflection.MethodInfo getMethod)
        {
            var dynamicMethod = EmitUtils.CreateDynamicMethod(string.Format("{0}_{1}", "$Get", propertyInfo.Name),
                                                              typeof(TReturn),
                                                              new[] { typeof(TSource) },
                                                              propertyInfo.DeclaringType);
            var il = dynamicMethod.GetILGenerator();

            if (!getMethod.IsStatic)
            {
                if (typeof(TSource).GetTypeInfo().IsValueType)
                {
                    il.Ldarga_S(0);
                }
                else
                {
                    il.Ldarg_0();
                    il.CastReference(propertyInfo.DeclaringType);
                }
            }

            il.CallMethod(getMethod);

            if (!typeof(TReturn).GetTypeInfo().IsValueType&& propertyInfo.PropertyType.GetTypeInfo().IsValueType)
            {
                il.Box(propertyInfo.PropertyType);
            }

            il.Ret();

            return((System.Func <TSource, TReturn>)dynamicMethod.CreateDelegate(typeof(System.Func <TSource, TReturn>)));
        }
        public static System.Func <Type> CreateDelegate <Type>(System.Type type)
        {
            if (type == null)
            {
                throw new System.ArgumentNullException(nameof(type));
            }

            var typeInfo = type.GetTypeInfo();

            if (typeInfo.IsInterface)
            {
                throw new System.ArgumentException(string.Format("{0} is an interface.", type), nameof(type));
            }

            if (typeInfo.IsAbstract)
            {
                throw new System.ArgumentException(string.Format("{0} is abstract.", type), nameof(type));
            }

            System.Reflection.ConstructorInfo constructorInfo = null;
            if (typeInfo.IsClass)
            {
                constructorInfo = type.GetConstructor(System.Type.EmptyTypes);

                if (constructorInfo == null)
                {
                    throw new System.ArgumentException(string.Format("{0} does not have a public parameterless constructor.", type), nameof(type));
                }
            }

            var dynamicMethod = EmitUtils.CreateDynamicMethod(string.Format("{0}_{1}", "$Create", type), typeof(Type), System.Type.EmptyTypes, type);
            var il            = dynamicMethod.GetILGenerator();

            if (typeInfo.IsClass)
            {
                il.Newobj(constructorInfo);
            }
            else
            {
                il.DeclareLocal(type);
                il.LoadLocalVariableAddress(0);
                il.Initobj(type);

                il.LoadLocalVariable(0);
                il.Box(type);
            }

            il.Ret();
            return((System.Func <Type>)dynamicMethod.CreateDelegate(typeof(System.Func <Type>)));
        }
Beispiel #4
0
        private static System.Func <TSource, TReturn> EmitFieldGetter <TSource, TReturn>(System.Reflection.FieldInfo fieldInfo)
        {
            if (fieldInfo.IsStatic && fieldInfo.IsLiteral)
            {
                return(null);
            }

            var dynamicMethod = EmitUtils.CreateDynamicMethod(string.Format("{0}_{1}", "$Get", fieldInfo.Name),
                                                              typeof(TReturn),
                                                              new[] { typeof(TSource) },
                                                              fieldInfo.DeclaringType);
            var il = dynamicMethod.GetILGenerator();

            if (fieldInfo.IsStatic)
            {
                il.Ldsfld(fieldInfo);
            }
            else
            {
                if (typeof(TSource).GetTypeInfo().IsValueType)
                {
                    il.Ldarga_S(0);
                }
                else
                {
                    il.Ldarg_0();
                    il.CastReference(fieldInfo.DeclaringType);
                }

                il.Ldfld(fieldInfo);
            }

            if (!typeof(TReturn).GetTypeInfo().IsValueType&& fieldInfo.FieldType.GetTypeInfo().IsValueType)
            {
                il.Box(fieldInfo.FieldType);
            }
            il.Ret();

            return((System.Func <TSource, TReturn>)dynamicMethod.CreateDelegate(typeof(System.Func <TSource, TReturn>)));
        }
Beispiel #5
0
        private static System.Action <TTarget, TValue> EmitFieldSetter <TTarget, TValue>(System.Reflection.FieldInfo fieldInfo)
        {
            if (fieldInfo.IsStatic && fieldInfo.IsLiteral)
            {
                return(null);
            }

            var dynamicMethod = EmitUtils.CreateDynamicMethod(string.Format("{0}_{1}", "$Set", fieldInfo.Name),
                                                              null,
                                                              new[] { typeof(TTarget), typeof(TValue) },
                                                              fieldInfo.DeclaringType);
            var il = dynamicMethod.GetILGenerator();

            il.DeclareLocal(fieldInfo.FieldType);
            il.Ldarg_1();
            if (!typeof(TValue).GetTypeInfo().IsValueType)
            {
                il.CastValue(fieldInfo.FieldType);
            }
            il.Stloc_0();

            if (fieldInfo.IsStatic)
            {
                il.Ldloc_0();
                il.Stsfld(fieldInfo);
            }
            else
            {
                il.Ldarg_0();
                il.CastReference(fieldInfo.DeclaringType);
                il.Ldloc_0();
                il.Stfld(fieldInfo);
            }

            il.Ret();
            return((System.Action <TTarget, TValue>)dynamicMethod.CreateDelegate(typeof(System.Action <TTarget, TValue>)));
        }
        public static System.Func <object[], object> CreateDelegate(System.Reflection.ConstructorInfo constructorInfo, bool validateArguments = true)
        {
            if (constructorInfo == null)
            {
                throw new System.ArgumentNullException(nameof(constructorInfo));
            }

            var delclaringType = constructorInfo.DeclaringType.GetTypeInfo();

            if (delclaringType.IsAbstract)
            {
                throw new System.ArgumentException("The declaring type of the constructor is abstract.", nameof(constructorInfo));
            }

            var dynamicMethod = EmitUtils.CreateDynamicMethod(string.Format("{0}_{1}", "$Create", delclaringType.Name),
                                                              typeof(object),
                                                              new[] { typeof(object[]) },
                                                              constructorInfo.DeclaringType);
            var il = dynamicMethod.GetILGenerator();

            var args = constructorInfo.GetParameters();
            var lableValidationCompleted = il.DefineLabel();

            if (!validateArguments || args.Length == 0)
            {
                il.Br_S(lableValidationCompleted);
            }
            else
            {
                var lableCheckArgumentsLength = il.DefineLabel();

                il.Ldarg_0();
                il.Brtrue_S(lableCheckArgumentsLength);

                il.ThrowArgumentsNullExcpetion("arguments");

                il.MarkLabel(lableCheckArgumentsLength);
                il.Ldarg_0();
                il.Ldlen();
                il.Conv_I4();
                il.LoadInt32(args.Length);
                il.Bge_S(lableValidationCompleted);

                il.ThrowArgumentsExcpetion("Not enough arguments in the argument array.", "arguments");
            }

            il.MarkLabel(lableValidationCompleted);
            if (args.Length > 0)
            {
                for (int i = 0; i < args.Length; i++)
                {
                    il.Ldarg_0();
                    il.LoadInt32(i);
                    il.Ldelem_Ref();
                    il.CastValue(args[i].ParameterType);
                }
            }
            il.Newobj(constructorInfo);
            il.Ret();

            return((System.Func <object[], object>)dynamicMethod.CreateDelegate(typeof(System.Func <object[], object>)));
        }