Let's say I have an generic interface IService<T>
and a class implementing it Service : IService<Bar>
I create a proxy of that interface :
var proxy = new DynamicProxy<IService<Bar>>(new Service()).GetTransparentProxy() as IService<Bar>;
DynamicProxy is a simple implementation of RealProxy :
public class DynamicProxy<I> : RealProxy
{
private I _decorated;
public DynamicProxy(I decorated) : base(typeof(I))
{
this._decorated = decorated;
}
public override IMessage Invoke(IMessage msg)
{
IMethodCallMessage methodCall = (IMethodCallMessage)msg;
MethodInfo methodInfo = methodCall.MethodBase as MethodInfo;
return new ReturnMessage(
methodInfo.Invoke(this._decorated, methodCall.InArgs),
null,
0,
methodCall.LogicalCallContext,
methodCall);
}
}
When using my proxy directly it works fine :
IEnumerable<Bar> bars = new List<Bar>() { new Bar { id = 2 }, new Bar { id = 3 } };
proxy.Foo(bars.First());
or even with a lambda, it's fine :
var data = bars.ToList().Select(bar => proxy.Foo(bar)).ToList();
But when used with a method group, it throws a target exception
var data = bars.ToList().Select(proxy.Foo).ToList();
The thrown exception :
{System.Reflection.TargetException: Object does not match target type.
at System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target)
at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
It seems that the realproxy doesn't manage to get the correct type of the generic :
The MethodBase of the IMethodCallMessage is {Int32 Foo(System.__Canon)}
instead of {Int32 Foo(Bar)}
Is it a limitation of the method group ? or a bug in the RealProxy implementation ?
Aucun commentaire:
Enregistrer un commentaire