首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Fody MethodDecorator不工作

Fody MethodDecorator不工作
EN

Stack Overflow用户
提问于 2016-07-19 03:01:51
回答 1查看 1.5K关注 0票数 0

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

我特别注意不要将我的IMethodDecorator包装在任何名称空间中,就像很多在线地方提到的那样。下面是我在控制台应用程序中尝试的示例代码。

IMethodDecorator

代码语言:javascript
复制
using System;
using System.Reflection;


    public interface IMethodDecorator
    {
        void OnEntry(MethodBase method);
        void OnExit(MethodBase method);
        void OnException(MethodBase method, Exception exception);
    }

MethodDecoratorAttribute

代码语言:javascript
复制
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");
        }
    }
}

有人能给我指明正确的方向吗。它看起来很容易实现,我知道我在某个地方犯了一个非常愚蠢的错误。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-07-19 07:24:32

显然,MethodDecorator.Fody的最新版本(目前版本为0.9.0.6 )无法工作。将版本降级为0.8.1.1版本为我解决了这个问题。

经过进一步的研究,两个版本的接口方法签名似乎是不同的。因此,当我有了新包时,它并没有期望MethodBase作为参数,而且由于找不到任何与它所期望的接口匹配的东西,所以它抛出了错误。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38448905

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档