我有一个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();
答案 0 :(得分:1)
也许方法没有正文(抽象,接口方法,部分,原生)?