Example #1
0
        /* 读取属性
         * // IL代码: 实际访问 get_PropertyName() 方法;
         *
         * 1. 载入第一参数 (隐藏实例变量)
         *    ldarg.0
         * 2. 转换实例类型: (如果需要)
         *
         *
         * 3. 读取属性:
         *    1). 实例: callvirt instance {MethodReturnTypeName} {TargetTypeName::get_PropertyName()}
         *    2). 静态: call {MethodReturnTypeName} {TargetTypeName::get_PropertyName()}
         *
         * 4. 转换结果类型: (如果需要)
         *
         * 5. 退出方法
         *    ret
         */
        Delegate InternalPropertyGetWithDynamic(PropertyInfo member, Type instance, Type value)
        {
            var method = member.GetGetMethod(true);

            var property = member.PropertyType;

            if (value == null)
            {
                value = property;
            }

            if (!method.IsStatic)
            {
                var reflected = method.ReflectedType;
                if (instance == null)
                {
                    instance = reflected;
                }

                var @dynamic = CreateDynamicMethod(value, new[] { instance }, _DynamicOptionsProperty, member);
                var il       = @dynamic.GetILGenerator();

                il.Emit(OpCodes.Ldarg_0);
                ConvertIfNeeded(il, reflected, instance); //

                if (reflected.IsValueType)
                {
                    il.EmitCall(OpCodes.Call, method, null);
                }
                else
                {
                    il.EmitCall(OpCodes.Callvirt, method, null);
                }

                ConvertIfNeeded(il, value, property); //

                il.Emit(OpCodes.Ret);

                return(@dynamic.CreateDelegate(TypeMemberGetInstance.MakeGenericType(new[] { instance, value })));
            }
            else
            {
                if (instance != null)
                {
                    throw Error.StaticObjectCannotConvertType("instance");
                }

                var @dynamic = CreateDynamicMethod(value, null, _DynamicOptionsProperty, member);
                var il       = @dynamic.GetILGenerator();

                il.EmitCall(OpCodes.Call, method, null);
                ConvertIfNeeded(il, value, property); //

                il.Emit(OpCodes.Ret);

                return(@dynamic.CreateDelegate(TypeMemberGetStatic.MakeGenericType(new[] { value })));
            }
        }
Example #2
0
        /* 读取字段
         * IL:
         * TField Function(TInstance instance) {
         * 1. 载入第一参数:
         *    ldarg.0
         * 2. 转换实例类型: (如果需要)
         *
         *
         * 3. 读取字段:
         *    {OpCode} {FieldTypeName} {TargetTypeName::FieldName}
         *    {OpCode}:
         *      1). 实例: ldfld;
         *      2). 静态: ldsfld;
         *    {FieldTypeName}:
         *      1). 标量类型: {TypeName};
         *      2). 引用类型: class {TypeName}
         *      3). 数值类型: valuetype {TypeName}
         * 4. 转换结果类型: (如果需要)
         *
         *
         * 5. 退出方法
         *    ret
         * }
         */
        Delegate InternalFieldGetWithDynamic(FieldInfo member, Type instance, Type value)
        {
            var field = member.FieldType;

            if (value == null)
            {
                value = field;
            }

            if (!member.IsStatic)
            {
                var reflected = member.ReflectedType;
                if (instance == null)
                {
                    instance = reflected;
                }

                var @dynamic = CreateDynamicMethod(value, new[] { instance }, _DynamicOptionsField, member);
                var il       = @dynamic.GetILGenerator();

                il.Emit(OpCodes.Ldarg_0);
                ConvertIfNeeded(il, reflected, instance); //

                il.Emit(OpCodes.Ldfld, member);
                ConvertIfNeeded(il, value, field); //

                il.Emit(OpCodes.Ret);

                return(@dynamic.CreateDelegate(TypeMemberGetInstance.MakeGenericType(new[] { instance, value })));
            }
            else
            {
                if (instance != null)
                {
                    throw new ArgumentException();
                }

                var @dynamic = CreateDynamicMethod(value, null, _DynamicOptionsField, member);
                var il       = @dynamic.GetILGenerator();

                il.Emit(OpCodes.Ldsfld, member);
                ConvertIfNeeded(il, value, field); //

                il.Emit(OpCodes.Ret);

                return(@dynamic.CreateDelegate(TypeMemberGetStatic.MakeGenericType(new[] { value })));
            }
        }