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(); }
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); } }
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); } }
public override void GenerateConversion(IProxyModuleCoderAccess proxyModule, ILGenerator gen) { var proxyType = proxyModule.GetTypeFromProxyClassDescriptor(_pcd); var ctor = proxyType.GetConstructor(new[] { _fromType }); gen.Emit(OpCodes.Newobj, ctor); }
public override object GenerateInConversion(Action load, IProxyModuleCoderAccess proxyModule, ILGenerator gen) { var local = gen.DeclareLocal(_toType); gen.Emit(OpCodes.Ldloca_S, local); return(local); }
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); }
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); }
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); }
public SafeDirectProxySubjectCoder(IRealSubjectMixinCoder rsmc, IProxyModuleCoderAccess proxyModule, Type subjectType) { if (rsmc==null) throw new ArgumentNullException("rsmc"); _rsmc = rsmc; _proxyModule = proxyModule; _subjectType = subjectType; }
public SafeDirectProxySubjectCoder(IRealSubjectMixinCoder rsmc, IProxyModuleCoderAccess proxyModule, Type subjectType) { if (rsmc == null) { throw new ArgumentNullException("rsmc"); } _rsmc = rsmc; _proxyModule = proxyModule; _subjectType = subjectType; }
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]) }
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()); }
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); }
public abstract void GenerateConversion(IProxyModuleCoderAccess proxyModule, ILGenerator gen);
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); }
public override object GenerateInConversion(Action load, IProxyModuleCoderAccess proxyModule, ILGenerator gen) { throw new InvalidOperationException("Cannot generate an in-conversion for a not bindable param"); }
protected internal ProxyClassCoder(IProxyModuleCoderAccess pm, ProxyClassDescriptor pcd) { _pm = pm; _mb = pm.ModuleBuilder; _pcd = pcd; }
public override void GenerateOutConversion(object token, Action load, IProxyModuleCoderAccess proxyModule, ILGenerator gen) { }
public override void GenerateConversion(IProxyModuleCoderAccess proxyModule, ILGenerator gen) { var proxyType = proxyModule.GetTypeFromProxyClassDescriptor(_pcd); var ctor = proxyType.GetConstructor(new[] {_fromType}); gen.Emit(OpCodes.Newobj, ctor); }
public abstract object GenerateInConversion(Action load, IProxyModuleCoderAccess proxyModule, ILGenerator gen);
public DuckProxySubjectMethodExistsCoder(IProxyModuleCoderAccess proxyModule, Type subjectType, Type realSubjectType) { _proxyModule = proxyModule; _subjectType = subjectType; _realSubjectType = realSubjectType; }
public SafeNullProxySubjectCoder(IProxyModuleCoderAccess proxyModule) { _proxyModule = proxyModule; }
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"); }
public override void GenerateConversion(IProxyModuleCoderAccess proxyModule, ILGenerator gen) { }
public override void GenerateConversion(IProxyModuleCoderAccess proxyModule, ILGenerator gen) { throw new InvalidOperationException("Cannot generate a conversion for a not bindable value"); }
public override object GenerateInConversion(Action load, IProxyModuleCoderAccess proxyModule, ILGenerator gen) { var local = gen.DeclareLocal(_toType); gen.Emit(OpCodes.Ldloca_S, local); return local; }
public override object GenerateInConversion(Action load, IProxyModuleCoderAccess proxyModule, ILGenerator gen) { load(); _valueBinding.GenerateConversion(proxyModule, gen); return(null); }
public DuckProxySubjectCoder(IRealSubjectMixinCoder rsmc, IProxyModuleCoderAccess proxyModule, DuckProxySubject subject) { _rsmc = rsmc; _proxyModule = proxyModule; _bindings = subject.Type.GetMethods().ToDictionary(mi => mi, subject.GetBestMatch); }
public abstract void GenerateOutConversion(object token, Action load, IProxyModuleCoderAccess proxyModule, ILGenerator gen);
public override object GenerateInConversion(Action load, IProxyModuleCoderAccess proxyModule, ILGenerator gen) { load(); _valueBinding.GenerateConversion(proxyModule, gen); return null; }
public override void GenerateCall(IProxyModuleCoderAccess proxyModule, ILGenerator gen) { throw new InvalidOperationException("Cannot generate a call for not bindable method"); }
public override void GenerateConversion(IProxyModuleCoderAccess proxyModule, ILGenerator gen) { gen.Emit(OpCodes.Call, _method); }
/// <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);