protected object AttemptMethodCall(MethodInfo adaptee, MethodInfo candidate, params object[] args) { var proxyModule = ProxyModule.Default; var name = "MethodCaller_" + adaptee.Name + "_" + candidate.Name; var tb = proxyModule.ModuleBuilder.DefineType(name); var cb = tb.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new[] { typeof(StandardMethodBindingTests) }); var field = tb.DefineField("_t", typeof(StandardMethodBindingTests), FieldAttributes.Private); GenerateCtor(cb.GetILGenerator(), field); var mb = tb.DefineMethod("Call", MethodAttributes.Public, adaptee.ReturnType, adaptee.GetParameters().Select(p => p.ParameterType).ToArray()); var gen = mb.GetILGenerator(); gen.Emit(OpCodes.Ldarg_0); gen.Emit(OpCodes.Ldfld, field); // Use binding to generate the body of this method for testing var binding = StandardMethodBinding.TryBind(adaptee, candidate); Assert.That(binding.Bindable); Assert.That(binding.Score, Is.GreaterThan(DuckMethodBindingOption.NotBindable.Score)); binding.GenerateCall(proxyModule, gen); gen.Emit(OpCodes.Ret); #if FEATURE_LEGACYREFLECTION var mcType = tb.CreateType(); #else var mcType = tb.CreateTypeInfo().AsType(); #endif object mc = Activator.CreateInstance(mcType, this); var callMethod = mcType.GetMethod("Call"); return(callMethod.Invoke(mc, args)); }
public void TryBindReturnsNullIfArgNotBindable() { // Single arg does not match Assert.That(StandardMethodBinding.TryBind(GetMethod("VoidMethodOneArgA"), GetMethod("VoidMethodOneArgC")), Is.Null); // Both args do not match Assert.That(StandardMethodBinding.TryBind(GetMethod("VoidMethodTwoArgA"), GetMethod("VoidMethodTwoArgC")), Is.Null); // First arg matches, second does not Assert.That(StandardMethodBinding.TryBind(GetMethod("VoidMethodTwoArgA"), GetMethod("VoidMethodTwoArgD")), Is.Null); // Second arg matches, first does not Assert.That(StandardMethodBinding.TryBind(GetMethod("VoidMethodTwoArgA"), GetMethod("VoidMethodTwoArgE")), Is.Null); }
public void TryBindReturnsNullIfRetValNotBindable() { Assert.That(StandardMethodBinding.TryBind(GetMethod("VoidMethodZeroArgA"), GetMethod("IntMethodZeroArgA")), Is.Null); Assert.That(StandardMethodBinding.TryBind(GetMethod("IntMethodZeroArgA"), GetMethod("ObjectMethodZeroArgA")), Is.Null); }
public void TryBindReturnsNullIfArgCountDiffers() { Assert.That(StandardMethodBinding.TryBind(GetMethod("VoidMethodZeroArgA"), GetMethod("VoidMethodOneArgA")), Is.Null); Assert.That(StandardMethodBinding.TryBind(GetMethod("VoidMethodOneArgA"), GetMethod("VoidMethodTwoArgA")), Is.Null); }