假设我有一个通用接口IService<T>和一个实现它的类Service : IService<Bar>
我创建该接口的代理:

var proxy = new DynamicProxy<IService<Bar>>(new Service()).GetTransparentProxy() as IService<Bar>;

dynamicProxy是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);
        }
    }

当直接使用我的代理时,它可以正常工作:
IEnumerable<Bar> bars = new List<Bar>() { new Bar { id = 2 }, new Bar { id = 3 } };
proxy.Foo(bars.First());

或者即使有一只小羊羔,也没关系:
 var data = bars.ToList().Select(bar => proxy.Foo(bar)).ToList();

但是当与方法组一起使用时,它抛出一个目标异常
var data = bars.ToList().Select(proxy.Foo).ToList();

引发的异常:
{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)

realproxy似乎无法获得正确的泛型类型:
imethodCallMessage的methodBase是{Int32 Foo(System.__Canon)}而不是{Int32 Foo(Bar)}
是方法组的限制吗?或者realproxy实现中的一个bug?
你可以在这里看到:
https://dotnetfiddle.net/w2VlVN
MSDN forum中没有运气,我怎样才能打开一个bug?

最佳答案

问题是:

methodInfo.Invoke(this._decorated, methodCall.InArgs),

这里的this参数是错误的,您需要直接调用类,而不是使用this
不使用_decorated直接尝试通过this

09-05 03:19