MethodInfo.GetMethodBody возвращает ноль

У меня есть 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.
        }
    }

}

Похоже, это проблема Castle DynamicProxy:

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();

1 ответ

Может быть, метод dosent имеет тело (абстрактный, интерфейсный метод, частичный, нативный)?

Другие вопросы по тегам