How to use MethodDecorator.Fody Decorator in another project(如何在另一个项目中使用MethodDecorator.Fody Decorator)
问题描述
我使用Fody Nuget包的方法如下
安装程序包
PM> Install-Package MethodDecorator.Fody
修饰方法
public class BusinessLayerClass { [LogMethod] public string BusinessLayerMethod() { DataLayerClass dataLayerClass = new DataLayerClass(); return dataLayerClass.DataLayerMethod(); } }
编写拦截器
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); } }
这在同一项目中工作得很好,但当我在另一个项目的另一个方法中使用修饰符[LogMethod]时,此OnEntry()
、OnExit()
、OnException(Exception exception)
方法不激发。
例如:
[LogMethod]
public void Another_Method_In_Seperate_Project()
我添加了对定义了[LogMethod]的项目的引用。
有没有人可以给我一个方法,让我在其他项目中使用相同的实现,而不是在每个项目中实现LogMethodAttribute.cs(其中定义了这个[LogMethod])。
推荐答案
您还需要在其他项目中安装MethodDecorator.Fody
Nuget包(以及依赖项)。您还需要在该项目中添加另一个FodyWeavers.xml
。
这篇关于如何在另一个项目中使用MethodDecorator.Fody Decorator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在另一个项目中使用MethodDecorator.Fody Decorator


基础教程推荐
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01