Fody MethodDecorator 不工作
Fody MethodDecorator not working
我正在尝试使用 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");
}
}
}
有人能指出我正确的方向吗?它看起来很容易实现,我知道我在某个地方犯了一个非常愚蠢的错误。
显然最新版本的 MethodDecorator.Fody(当前版本 0.9.0.6)无法正常工作。将版本降级到版本 0.8.1.1 为我解决了这个问题。
经过更多调查,两个版本的接口方法签名似乎不同。因此,当我拥有新包时,它并不期望 MethodBase 作为参数,并且由于找不到任何与它期望的接口匹配的东西,它抛出了错误。
我正在尝试使用 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");
}
}
}
有人能指出我正确的方向吗?它看起来很容易实现,我知道我在某个地方犯了一个非常愚蠢的错误。
显然最新版本的 MethodDecorator.Fody(当前版本 0.9.0.6)无法正常工作。将版本降级到版本 0.8.1.1 为我解决了这个问题。
经过更多调查,两个版本的接口方法签名似乎不同。因此,当我拥有新包时,它并不期望 MethodBase 作为参数,并且由于找不到任何与它期望的接口匹配的东西,它抛出了错误。