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