C# MethodInfo.GetMethodBody返回null
我有一个C# MethodInfo.GetMethodBody返回null,c#,reflection,ninject,castle-dynamicproxy,ninject-interception,C#,Reflection,Ninject,Castle Dynamicproxy,Ninject Interception,我有一个IInvocation(来自Ninject.Extensions.Interception),它有一个.Request.Method,指向我在应用程序中创建的类上的一个方法(因此,自定义,而不是核心.NET代码中的任何内容)。当我调用invocation.Request.Method.GetMethodBody()时,它返回为null。为什么? using Ninject; using Ninject.Extensions.Interception; using Ninject.Exte
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.
}
}
}
使用Ninject;
使用Ninject.Extensions.Interception;
使用Ninject.Extensions.Interception.Infrastructure.Language;
命名空间ShortButComplete
{
班级计划
{
静态void Main(字符串[]参数)
{
IKernel kernel=新的标准内核();
kernel.Bind().To().Intercept().With();
var result=kernel.Get().UhOh();
}
}
公共类MethodReader:IInterceptor
{
公共无效拦截(IInvocation调用)
{
var uhoh=invocation.Request.Method.GetMethodBody();
//以上为空。
invocation.procedure();
}
}
公共接口IMethodHolder
{
int-UhOh();
}
公共类方法持有者:IMethodHolder
{
公共卫生
{
返回4;//由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();
使用Castle.DynamicProxy;
//使用Ninject;
//使用Ninject.Extensions.Interception;
//使用Ninject.Extensions.Interception.Infrastructure.Language;
//IKernel kernel=新的标准内核();
//kernel.Bind().To().Intercept().With();
//var result=kernel.Get().UhOh();
var real=new MethodHolder();
var proxy=new ProxyGenerator().CreateInterfaceProxyWithTarget(real,new MethodReader());
var result=proxy.UhOh();
可能方法没有正文(抽象、接口方法、部分、本机)?您能用一个简短但完整的程序来演示这一点吗?请阅读GetMethodBody
通常为extern
方法返回null。这里是这样吗?@JonSkeet:好的。几分钟后回来@添加了简短但完整的程序。