How to use MethodDecorator.Fody Decorator in another project

会有一股神秘感。 提交于 2020-04-30 05:41:46

问题


I used Fody Nuget package as follows

  1. Install package

    PM> Install-Package MethodDecorator.Fody
    
  2. Decorate the method

    public class BusinessLayerClass
    {
        [LogMethod] 
        public string BusinessLayerMethod()
        {
            DataLayerClass dataLayerClass = new DataLayerClass();
           return  dataLayerClass.DataLayerMethod();
        }
    }
    
  3. Write the interceptor

    using System;
    using System.Reflection;
    
    [module: LogMethod] // Attribute should be "registered" by adding as module or assembly custom attribute
    
    // Any attribute which provides OnEntry/OnExit/OnException with proper args
    [AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Assembly | AttributeTargets.Module)]
    public class LogMethodAttribute : Attribute, IMethodDecorator
    {
        private MethodBase _method;
    
        // instance, method and args can be captured here and stored in attribute instance fields
        // for future usage in OnEntry/OnExit/OnException
    
        public void Init(object instance, MethodBase method, object[] args)
        {
            _method = method;
        }
    
        public void OnEntry()
        {
            NLogging.Trace("Entering into {0}", _method.Name);
        }
    
        public void OnExit()
        {
            NLogging.Trace("Exiting into {0}", _method.Name);
        }
    
        public void OnException(Exception exception)
        {
            NLogging.Trace(exception, "Exception {0}", _method.Name);
        }
    }
    

This works fine within the same project but when I use the decorator [LogMethod] in another method in another project, this OnEntry(), OnExit(), OnException(Exception exception) methods do not fire.

For instance:

[LogMethod]
public void Another_Method_In_Seperate_Project()

I added a reference to the project where [LogMethod] is defined.

Could anyone please send me a way of using the same implementation in other projects without doing the implementation of LogMethodAttribute.cs (where this [LogMethod] is defined) in each and every project.


回答1:


You need to install MethodDecorator.Fody nuget package in the other project as well (along with dependencies). You will also need to add another FodyWeavers.xml in that project.



来源:https://stackoverflow.com/questions/49648179/how-to-use-methoddecorator-fody-decorator-in-another-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!