コード例 #1
0
        void GenerateTargetAccessType(IProxyModuleCoderAccess proxyModule, IEnumerable <MethodInfo> methodInfos)
        {
            var    mb       = proxyModule.ModuleBuilder;
            string typeName = proxyModule.AssemblyName + ".TargetAccess_" + Guid.NewGuid().ToString("N");
            var    tb       = mb.DefineType(typeName, TypeAttributes.Class | TypeAttributes.Abstract | TypeAttributes.Sealed);

            var staticCons = tb.DefineConstructor(MethodAttributes.Static, CallingConventions.Standard, null);
            var gen        = staticCons.GetILGenerator();

            var createDelegateMethod = typeof(Delegate).GetMethod("CreateDelegate", new[] { typeof(Type), typeof(MethodInfo) });

            foreach (var methodInfo in methodInfos)
            {
                var delegateType  = DeclareDelegateType(proxyModule, methodInfo);
                var delegateField = tb.DefineField("_" + methodInfo.GetMetadataToken(), delegateType, FieldAttributes.Public | FieldAttributes.Static);

                gen.EmitLdType(delegateType);
                gen.EmitLdMethod(methodInfo);
                gen.Emit(OpCodes.Call, createDelegateMethod);
                gen.Emit(OpCodes.Castclass, delegateType);
                gen.Emit(OpCodes.Stsfld, delegateField);
            }

            gen.Emit(OpCodes.Ret);
            _targetAccessType = tb.CreateType();
        }
コード例 #2
0
        public override void GenerateConversion(IProxyModuleCoderAccess proxyModule, ILGenerator gen)
        {
            var toTypeCtor = _toType.GetConstructor(new[] { _coreToType });

            if (!_fromTypeNullable)
            {
                _coreBinding.GenerateConversion(proxyModule, gen);
                gen.Emit(OpCodes.Newobj, toTypeCtor);
            }
            else
            {
                var fromLocal     = gen.DeclareLocal(_fromType);
                var toLocal       = gen.DeclareLocal(_toType);
                var hasValueLabel = gen.DefineLabel();
                var doneLabel     = gen.DefineLabel();
                gen.Emit(OpCodes.Stloc, fromLocal);
                gen.Emit(OpCodes.Ldloca, fromLocal);
                gen.Emit(OpCodes.Call, _fromType.GetMethod("get_HasValue"));
                gen.Emit(OpCodes.Brtrue, hasValueLabel);
                gen.Emit(OpCodes.Ldloca, toLocal);
                gen.Emit(OpCodes.Initobj, _toType);
                gen.Emit(OpCodes.Ldloc, toLocal);
                gen.Emit(OpCodes.Br, doneLabel);
                // :HasValue
                gen.MarkLabel(hasValueLabel);
                gen.Emit(OpCodes.Ldloca, fromLocal);
                gen.Emit(OpCodes.Call, _fromType.GetMethod("GetValueOrDefault", Type.EmptyTypes));
                _coreBinding.GenerateConversion(proxyModule, gen);
                gen.Emit(OpCodes.Newobj, toTypeCtor);
                // :Done
                gen.MarkLabel(doneLabel);
            }
        }
コード例 #3
0
 public override void GenerateConversion(IProxyModuleCoderAccess proxyModule, ILGenerator gen)
 {
     var toTypeCtor = _toType.GetConstructor(new[] {_coreToType});
     if (!_fromTypeNullable)
     {
         _coreBinding.GenerateConversion(proxyModule, gen);
         gen.Emit(OpCodes.Newobj, toTypeCtor);
     }
     else
     {
         var fromLocal = gen.DeclareLocal(_fromType);
         var toLocal = gen.DeclareLocal(_toType);
         var hasValueLabel = gen.DefineLabel();
         var doneLabel = gen.DefineLabel();
         gen.Emit(OpCodes.Stloc, fromLocal);
         gen.Emit(OpCodes.Ldloca, fromLocal);
         gen.Emit(OpCodes.Call, _fromType.GetMethod("get_HasValue"));
         gen.Emit(OpCodes.Brtrue, hasValueLabel);
         gen.Emit(OpCodes.Ldloca, toLocal);
         gen.Emit(OpCodes.Initobj, _toType);
         gen.Emit(OpCodes.Ldloc, toLocal);
         gen.Emit(OpCodes.Br, doneLabel);
         // :HasValue
         gen.MarkLabel(hasValueLabel);
         gen.Emit(OpCodes.Ldloca, fromLocal);
         gen.Emit(OpCodes.Call, _fromType.GetMethod("GetValueOrDefault", Type.EmptyTypes));
         _coreBinding.GenerateConversion(proxyModule, gen);
         gen.Emit(OpCodes.Newobj, toTypeCtor);
         // :Done
         gen.MarkLabel(doneLabel);
     }
 }
コード例 #4
0
        public override void GenerateConversion(IProxyModuleCoderAccess proxyModule, ILGenerator gen)
        {
            var proxyType = proxyModule.GetTypeFromProxyClassDescriptor(_pcd);
            var ctor      = proxyType.GetConstructor(new[] { _fromType });

            gen.Emit(OpCodes.Newobj, ctor);
        }
コード例 #5
0
        public override object GenerateInConversion(Action load, IProxyModuleCoderAccess proxyModule, ILGenerator gen)
        {
            var local = gen.DeclareLocal(_toType);

            gen.Emit(OpCodes.Ldloca_S, local);
            return(local);
        }
コード例 #6
0
        public override void GenerateConversion(IProxyModuleCoderAccess proxyModule, ILGenerator gen)
        {
            if (_targetType == typeof(double) && _type == typeof(float))
            {
                return;
            }

            OpCode opCode;

            if (ConversionOpCodeByType.TryGetValue(_targetType, out opCode))
            {
                if (opCode != OpCodes.Nop)
                {
                    gen.Emit(opCode);
                }
                return;
            }

            VerifyTargetTypeIsDecimal();

            var ctorType = _type;

            if (_type == typeof(sbyte) || _type == typeof(short))
            {
                ctorType = typeof(int);
            }
            else if (_type == typeof(byte) || _type == typeof(char) || _type == typeof(ushort))
            {
                ctorType = typeof(uint);
            }

            var ctorDecimal = typeof(decimal).GetConstructor(new[] { ctorType });

            gen.Emit(OpCodes.Newobj, ctorDecimal);
        }
コード例 #7
0
        public override void GenerateCall(IProxyModuleCoderAccess proxyModule, ILGenerator gen)
        {
            var pars       = _adaptee.GetParameters();
            var targetPars = _target.GetParameters();

            var tokens = new object[pars.Length];

            // Generate in conversions
            for (ushort i = 0; i < pars.Length; ++i)
            {
                // ReSharper disable once AccessToModifiedClosure
                tokens[i] = _paramBindings[i].GenerateInConversion(() => gen.EmitBestLdArg((ushort)(i + 1)), proxyModule, gen);
            }

            gen.Emit(OpCodes.Callvirt, _target);

            // Generate out conversions
            for (ushort i = 0; i < pars.Length; ++i)
            {
                // ReSharper disable once AccessToModifiedClosure
                _paramBindings[i].GenerateOutConversion(tokens[i], () => gen.EmitBestLdArg((ushort)(i + 1)), proxyModule, gen);
            }

            _retValBinding.GenerateConversion(proxyModule, gen);
        }
コード例 #8
0
 public override void GenerateOutConversion(object token, Action load, IProxyModuleCoderAccess proxyModule, ILGenerator gen)
 {
     var local = (LocalBuilder)token;
     load();
     gen.Emit(OpCodes.Ldloc, local);
     _valueBinding.GenerateConversion(proxyModule, gen);
     gen.EmitStoreToRef(_fromType);
 }
コード例 #9
0
 public SafeDirectProxySubjectCoder(IRealSubjectMixinCoder rsmc, IProxyModuleCoderAccess proxyModule, Type subjectType)
 {
     if (rsmc==null)
         throw new ArgumentNullException("rsmc");
     _rsmc = rsmc;
     _proxyModule = proxyModule;
     _subjectType = subjectType;
 }
コード例 #10
0
        public override void GenerateOutConversion(object token, Action load, IProxyModuleCoderAccess proxyModule, ILGenerator gen)
        {
            var local = (LocalBuilder)token;

            load();
            gen.Emit(OpCodes.Ldloc, local);
            _valueBinding.GenerateConversion(proxyModule, gen);
            gen.EmitStoreToRef(_fromType);
        }
コード例 #11
0
 public SafeDirectProxySubjectCoder(IRealSubjectMixinCoder rsmc, IProxyModuleCoderAccess proxyModule, Type subjectType)
 {
     if (rsmc == null)
     {
         throw new ArgumentNullException("rsmc");
     }
     _rsmc        = rsmc;
     _proxyModule = proxyModule;
     _subjectType = subjectType;
 }
コード例 #12
0
 public override void GenerateConversion(IProxyModuleCoderAccess proxyModule, ILGenerator gen)
 {
     var target = gen.DeclareLocal(_fromType);
     gen.Emit(OpCodes.Stloc, target); // store the value to be converted
     var pmField = proxyModule.GetProxyModuleField();
     gen.Emit(OpCodes.Ldsfld, pmField); // push static ProxyModule instance for this ProxyModule
     gen.Emit(OpCodes.Call, FromProxyModuleMethod); // DuckFactory.FromProxyModule([s0])
     gen.Emit(OpCodes.Ldloc, target);
     var genMakeDuckProxyForMethod = MakeDuckProxyForMethod.MakeGenericMethod(_subjectType);
     gen.Emit(OpCodes.Callvirt, genMakeDuckProxyForMethod); // [s1].MakeDuckProxyFor<_fromType>([s0])
 }
コード例 #13
0
        public override void GenerateConversion(IProxyModuleCoderAccess proxyModule, ILGenerator gen)
        {
            var target = gen.DeclareLocal(_fromType);

            gen.Emit(OpCodes.Stloc, target); // store the value to be converted
            var pmField = proxyModule.GetProxyModuleField();

            gen.Emit(OpCodes.Ldsfld, pmField);             // push static ProxyModule instance for this ProxyModule
            gen.Emit(OpCodes.Call, FromProxyModuleMethod); // DuckFactory.FromProxyModule([s0])
            gen.Emit(OpCodes.Ldloc, target);
            var genMakeDuckProxyForMethod = MakeDuckProxyForMethod.MakeGenericMethod(_subjectType);

            gen.Emit(OpCodes.Callvirt, genMakeDuckProxyForMethod); // [s1].MakeDuckProxyFor<_fromType>([s0])
        }
コード例 #14
0
        Type DeclareDelegateType(IProxyModuleCoderAccess proxyModule, MethodInfo methodInfo)
        {
            var    mb        = proxyModule.ModuleBuilder;
            string typeName  = proxyModule.AssemblyName + ".Delegate_" + methodInfo.GetMetadataToken();
            var    baseType  = typeof(MulticastDelegate);
            var    tb        = mb.DefineType(typeName, TypeAttributes.AutoClass | TypeAttributes.Sealed | TypeAttributes.Public, baseType);
            var    ctorTypes = new[] { typeof(object), typeof(IntPtr) };
            var    ctor      = tb.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, ctorTypes);

            ctor.SetImplementationFlags(MethodImplAttributes.Runtime | MethodImplAttributes.Managed);
            DeclareBeginInvoke(tb, methodInfo);
            DeclareEndInvoke(tb, methodInfo);
            DeclareInvoke(tb, methodInfo);

            return(tb.CreateType());
        }
コード例 #15
0
        static FieldInfo GenerateStaticType(Type realSubjectType, Type subjectType, IProxyModuleCoderAccess proxyModule)
        {
            var    mb        = proxyModule.ModuleBuilder;
            var    fieldType = typeof(ISubjectMethodExists <>).MakeGenericType(subjectType);
            string typeName  = proxyModule.AssemblyName + ".SmeHolder_" + Guid.NewGuid().ToString("N");
            var    tb        = mb.DefineType(typeName, TypeAttributes.Class | TypeAttributes.Abstract | TypeAttributes.Sealed);
            var    field     = tb.DefineField("_i", fieldType, FieldAttributes.Static | FieldAttributes.Assembly);

            var staticCons = tb.DefineConstructor(MethodAttributes.Static, CallingConventions.Standard, null);
            var gen        = staticCons.GetILGenerator();

            var factoryMethod = typeof(DuckProxySubjectMethodExistsCoder).GetMethod("SubjectMethodExistsFactory")
                                .MakeGenericMethod(subjectType);

            gen.Emit(OpCodes.Ldsfld, proxyModule.GetProxyModuleField());
            gen.EmitLdType(realSubjectType);
            gen.EmitCall(OpCodes.Call, factoryMethod, null);
            gen.Emit(OpCodes.Stsfld, field);
            gen.Emit(OpCodes.Ret);
            tb.CreateType();
            return(field);
        }
コード例 #16
0
 public abstract void GenerateConversion(IProxyModuleCoderAccess proxyModule, ILGenerator gen);
コード例 #17
0
        public override void GenerateConversion(IProxyModuleCoderAccess proxyModule, ILGenerator gen)
        {
            if (_targetType==typeof(double) && _type==typeof(float))
                return;

            OpCode opCode;
            if (ConversionOpCodeByType.TryGetValue(_targetType, out opCode))
            {
                if (opCode!=OpCodes.Nop)
                    gen.Emit(opCode);
                return;
            }

            VerifyTargetTypeIsDecimal();

            var ctorType = _type;
            if (_type==typeof(sbyte) || _type==typeof(short))
                ctorType = typeof(int);
            else if (_type==typeof(byte) || _type==typeof(char) || _type==typeof(ushort))
                ctorType = typeof(uint);

            var ctorDecimal = typeof(decimal).GetConstructor(new[] {ctorType});
            gen.Emit(OpCodes.Newobj, ctorDecimal);
        }
コード例 #18
0
 public override object GenerateInConversion(Action load, IProxyModuleCoderAccess proxyModule, ILGenerator gen)
 {
     throw new InvalidOperationException("Cannot generate an in-conversion for a not bindable param");
 }
コード例 #19
0
 protected internal ProxyClassCoder(IProxyModuleCoderAccess pm, ProxyClassDescriptor pcd)
 {
     _pm  = pm;
     _mb  = pm.ModuleBuilder;
     _pcd = pcd;
 }
コード例 #20
0
 public override void GenerateOutConversion(object token, Action load, IProxyModuleCoderAccess proxyModule, ILGenerator gen)
 {
 }
コード例 #21
0
 public override void GenerateConversion(IProxyModuleCoderAccess proxyModule, ILGenerator gen)
 {
     var proxyType = proxyModule.GetTypeFromProxyClassDescriptor(_pcd);
     var ctor = proxyType.GetConstructor(new[] {_fromType});
     gen.Emit(OpCodes.Newobj, ctor);
 }
コード例 #22
0
 public abstract object GenerateInConversion(Action load, IProxyModuleCoderAccess proxyModule, ILGenerator gen);
コード例 #23
0
 public DuckProxySubjectMethodExistsCoder(IProxyModuleCoderAccess proxyModule, Type subjectType, Type realSubjectType)
 {
     _proxyModule     = proxyModule;
     _subjectType     = subjectType;
     _realSubjectType = realSubjectType;
 }
コード例 #24
0
 public SafeNullProxySubjectCoder(IProxyModuleCoderAccess proxyModule)
 {
     _proxyModule = proxyModule;
 }
コード例 #25
0
 public override void GenerateOutConversion(object token, Action load, IProxyModuleCoderAccess proxyModule, ILGenerator gen)
 {
     throw new InvalidOperationException("Cannot generate an out-conversion for a not bindable param");
 }
コード例 #26
0
 public override void GenerateConversion(IProxyModuleCoderAccess proxyModule, ILGenerator gen)
 {
 }
コード例 #27
0
 public override object GenerateInConversion(Action load, IProxyModuleCoderAccess proxyModule, ILGenerator gen)
 {
     throw new InvalidOperationException("Cannot generate an in-conversion for a not bindable param");
 }
コード例 #28
0
 public override void GenerateConversion(IProxyModuleCoderAccess proxyModule, ILGenerator gen)
 {
 }
コード例 #29
0
 public override void GenerateOutConversion(object token, Action load, IProxyModuleCoderAccess proxyModule, ILGenerator gen)
 {
     throw new InvalidOperationException("Cannot generate an out-conversion for a not bindable param");
 }
コード例 #30
0
 public SafeNullProxySubjectCoder(IProxyModuleCoderAccess proxyModule)
 {
     _proxyModule = proxyModule;
 }
コード例 #31
0
 public override void GenerateConversion(IProxyModuleCoderAccess proxyModule, ILGenerator gen)
 {
     throw new InvalidOperationException("Cannot generate a conversion for a not bindable value");
 }
コード例 #32
0
 public override object GenerateInConversion(Action load, IProxyModuleCoderAccess proxyModule, ILGenerator gen)
 {
     var local = gen.DeclareLocal(_toType);
     gen.Emit(OpCodes.Ldloca_S, local);
     return local;
 }
コード例 #33
0
 public override object GenerateInConversion(Action load, IProxyModuleCoderAccess proxyModule, ILGenerator gen)
 {
     load();
     _valueBinding.GenerateConversion(proxyModule, gen);
     return(null);
 }
コード例 #34
0
 public DuckProxySubjectCoder(IRealSubjectMixinCoder rsmc, IProxyModuleCoderAccess proxyModule, DuckProxySubject subject)
 {
     _rsmc        = rsmc;
     _proxyModule = proxyModule;
     _bindings    = subject.Type.GetMethods().ToDictionary(mi => mi, subject.GetBestMatch);
 }
コード例 #35
0
 public override void GenerateOutConversion(object token, Action load, IProxyModuleCoderAccess proxyModule, ILGenerator gen)
 {
 }
コード例 #36
0
 public abstract void GenerateOutConversion(object token, Action load, IProxyModuleCoderAccess proxyModule, ILGenerator gen);
コード例 #37
0
 public DuckProxySubjectCoder(IRealSubjectMixinCoder rsmc, IProxyModuleCoderAccess proxyModule, DuckProxySubject subject)
 {
     _rsmc = rsmc;
     _proxyModule = proxyModule;
     _bindings = subject.Type.GetMethods().ToDictionary(mi => mi, subject.GetBestMatch);
 }
コード例 #38
0
 public override object GenerateInConversion(Action load, IProxyModuleCoderAccess proxyModule, ILGenerator gen)
 {
     load();
     _valueBinding.GenerateConversion(proxyModule, gen);
     return null;
 }
コード例 #39
0
 public abstract void GenerateOutConversion(object token, Action load, IProxyModuleCoderAccess proxyModule, ILGenerator gen);
コード例 #40
0
 public override void GenerateCall(IProxyModuleCoderAccess proxyModule, ILGenerator gen)
 {
     throw new InvalidOperationException("Cannot generate a call for not bindable method");
 }
コード例 #41
0
 public abstract object GenerateInConversion(Action load, IProxyModuleCoderAccess proxyModule, ILGenerator gen);
コード例 #42
0
        public override void GenerateCall(IProxyModuleCoderAccess proxyModule, ILGenerator gen)
        {
            var pars = _adaptee.GetParameters();
            var targetPars = _target.GetParameters();

            var tokens = new object[pars.Length];

            // Generate in conversions
            for (ushort i = 0; i < pars.Length; ++i)
            {
                // ReSharper disable once AccessToModifiedClosure
                tokens[i] = _paramBindings[i].GenerateInConversion(() => gen.EmitBestLdArg((ushort)(i + 1)), proxyModule, gen);
            }

            gen.Emit(OpCodes.Callvirt, _target);

            // Generate out conversions
            for (ushort i = 0; i < pars.Length; ++i)
            {
                // ReSharper disable once AccessToModifiedClosure
                _paramBindings[i].GenerateOutConversion(tokens[i], () => gen.EmitBestLdArg((ushort)(i + 1)), proxyModule, gen);
            }

            _retValBinding.GenerateConversion(proxyModule, gen);
        }
コード例 #43
0
 public override void GenerateConversion(IProxyModuleCoderAccess proxyModule, ILGenerator gen)
 {
     gen.Emit(OpCodes.Call, _method);
 }
コード例 #44
0
 /// <summary>
 /// Must be called with the real subject on the stack of the body being generated.
 /// </summary>
 public abstract void GenerateCall(IProxyModuleCoderAccess proxyModule, ILGenerator gen);
コード例 #45
0
 public override void GenerateCall(IProxyModuleCoderAccess proxyModule, ILGenerator gen)
 {
     throw new InvalidOperationException("Cannot generate a call for not bindable method");
 }
コード例 #46
0
 public override void GenerateConversion(IProxyModuleCoderAccess proxyModule, ILGenerator gen)
 {
     throw new InvalidOperationException("Cannot generate a conversion for a not bindable value");
 }
コード例 #47
0
 public override void GenerateConversion(IProxyModuleCoderAccess proxyModule, ILGenerator gen)
 {
     gen.Emit(OpCodes.Call, _method);
 }