我正在尝试使用Fody创建一个方法装饰器,但是它给出了以下错误:

我特别注意不要将我的IMethodDecorator包装在任何名称空间中,就像很多在线地方提到的那样。下面是我在控制台应用程序中尝试的示例代码。
IMethodDecorator
using System;
using System.Reflection;
public interface IMethodDecorator
{
void OnEntry(MethodBase method);
void OnExit(MethodBase method);
void OnException(MethodBase method, Exception exception);
}MethodDecoratorAttribute
using System;
using System.Diagnostics;
using System.Reflection;
using FODYPOC;
// Atribute should be "registered" by adding as module or assembly custom attribute
[module: MethodDecorator]
namespace FODYPOC
{
// Any attribute which provides OnEntry/OnExit/OnException with proper args
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Assembly | AttributeTargets.Module)]
public class MethodDecoratorAttribute : Attribute, IMethodDecorator
{
// instance, method and args can be captured here and stored in attribute instance fields
// for future usage in OnEntry/OnExit/OnException
public MethodDecoratorAttribute() { }
public void OnEntry(MethodBase method)
{
Console.WriteLine();
}
public void OnExit(MethodBase method)
{
Console.WriteLine();
}
public void OnException(MethodBase method, Exception exception)
{
Console.WriteLine();
}
}
public class Sample
{
[MethodDecorator]
public void Method()
{
Debug.WriteLine("Your Code");
}
}
}有人能给我指明正确的方向吗。它看起来很容易实现,我知道我在某个地方犯了一个非常愚蠢的错误。
发布于 2016-07-19 07:24:32
显然,MethodDecorator.Fody的最新版本(目前版本为0.9.0.6 )无法工作。将版本降级为0.8.1.1版本为我解决了这个问题。
经过进一步的研究,两个版本的接口方法签名似乎是不同的。因此,当我有了新包时,它并没有期望MethodBase作为参数,而且由于找不到任何与它所期望的接口匹配的东西,所以它抛出了错误。
https://stackoverflow.com/questions/38448905
复制相似问题