我有一个IInvocation
(from 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();