MethodInfo.GetMethodBody returns null



我有一个IInvocation(来自Ninject.Extensions.Interception),它有一个.Request.Method指向我在应用程序中创建的类的方法(因此,自定义,而不是核心。net代码中的任何东西)。当我调用invocation.Request.Method.GetMethodBody()时,它返回为null。为什么?

using Ninject;
using Ninject.Extensions.Interception;
using Ninject.Extensions.Interception.Infrastructure.Language;
namespace ShortButComplete
{
    class Program
    {
        static void Main(string[] args)
        {
            IKernel kernel = new StandardKernel();
            kernel.Bind<IMethodHolder>().To<MethodHolder>().Intercept().With<MethodReader>();
            var result = kernel.Get<IMethodHolder>().UhOh();
        }
    }
    public class MethodReader : IInterceptor
    {
        public void Intercept(IInvocation invocation)
        {
            var uhoh = invocation.Request.Method.GetMethodBody();
            // The above is null.
            invocation.Proceed();
        }
    }
    public interface IMethodHolder
    {
        int UhOh();
    }
    public class MethodHolder : IMethodHolder
    {
        public int UhOh()
        {
            return 4; // Guaranteed random by roll of a d6.
        }
    }
}

看起来这是一个城堡动态代理问题:

using Castle.DynamicProxy;
//using Ninject;
//using Ninject.Extensions.Interception;
//using Ninject.Extensions.Interception.Infrastructure.Language;

//IKernel kernel = new StandardKernel();
//kernel.Bind<IMethodHolder>().To<MethodHolder>().Intercept().With<MethodReader>();
//var result = kernel.Get<IMethodHolder>().UhOh();
var real = new MethodHolder();
var proxy = new ProxyGenerator().CreateInterfaceProxyWithTarget<IMethodHolder>(real, new MethodReader());
var result = proxy.UhOh();

也许方法没有主体(抽象,接口方法,局部,本机)?

相关内容

  • 没有找到相关文章

最新更新