很久很久以前用过postsharp来做AOP, 大家知道的,现在那东东需要付费,于是尝试了一下Fody,但是发现Fody跟新太快了,所以大家在安装fody的时候尽力安装老的版本:packages.config

<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Cauldron.Interception.Fody" version="2.0.27" targetFramework="net461" />
<package id="Costura.Fody" version="1.6.2" targetFramework="net461" developmentDependency="true" />
<package id="Fody" version="2.5.0" targetFramework="net461" developmentDependency="true" />
</packages>

创建一个方法拦截的demo如下:

using Cauldron.Interception;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks; namespace FodyTest
{ [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
public class LoggerAttribute : Attribute, IMethodInterceptor
{
private string methodName; public void OnEnter(Type declaringType, object instance, MethodBase methodbase, object[] values)
{
this.methodName = methodbase.Name;
this.AppendToFile($"Enter -> {declaringType.Name} {methodbase.Name} {string.Join(" ", values)}");
} public void OnException(Exception e) => this.AppendToFile($"Exception -> {e.Message}"); public void OnExit() => this.AppendToFile($"Exit -> {this.methodName}"); private void AppendToFile(string line)
{
File.AppendAllLines("log.txt", new string[] { line });
Console.WriteLine(">> " + line);
}
}
}

属性拦截如下:

using Cauldron.Interception;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace FodyTest
{ [AttributeUsage(AttributeTargets.Property | AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
public sealed class OnPropertySetAttribute : Attribute, IPropertySetterInterceptor
{
[AssignMethod("{CtorArgument:0}")]
public Action<string, object> onSetMethod = null; public OnPropertySetAttribute(string methodName)
{
} public void OnException(Exception e)
{
} public void OnExit()
{
} public bool OnSet(PropertyInterceptionInfo propertyInterceptionInfo, object oldValue, object newValue)
{
this.onSetMethod?.Invoke(propertyInterceptionInfo.PropertyName, newValue);
return false;
}
}
}

创建FodyWeavers.xml:

<?xml version="1.0" encoding="utf-8"?>
<Weavers>
<Cauldron.Interception />
<Costura />
</Weavers>

调用code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace FodyTest
{
[OnPropertySet(nameof(ExecuteMe))]
public class PropertySetterTestClass
{
public int BookId { get; set; }
public string BookName { get; set; } private void ExecuteMe(string propertyName, object newValue) =>
Console.WriteLine($"The property '{propertyName}' has a new value: '{newValue ?? ""}'");
} [Logger]
internal class Program
{
private static int Add(int a, int b) => a + b; private static void Main(string[] args)
{
Console.WriteLine(Add(, )); var sampleClass = new PropertySetterTestClass
{
BookName = "50 shades of C#",
BookId =
}; Console.ReadLine();
}
}
}

运行效果:

AOP - C# Fody中的方法和属性拦截的更多相关文章

  1. JavaScript中的方法和属性

    书读百遍其义自见 学习<JavaScript设计模式>一书时,前两个章节中的讲解的JavaScript基础知识,让我对属性和方法有了清晰的认识.如下是我的心得体会以及部分摘录的代码. 不同 ...

  2. (转)C# Aop简单扫盲及ORM实体类属性拦截示例

    转自: http://www.cnblogs.com/cyq1162/archive/2012/05/30/2526573.html 先说下场景,C#中为什么要使用Aop,而我又是在哪里使用Aop? ...

  3. Postsharp基本用法——方法、属性拦截与异常处理

    以下Demo代码基于 .NET Core 演示了Postsharp的基本使用方法,稍作修改(反射部分有些许差异)也适用于.NET Framework. 更多高级使用方法详见官方文档.http://sa ...

  4. python-day33--Process类中的方法及属性

    p.daemon = True -->守护进程,守护进程不可以再有子进程,并且主进程死守护进程就死,要写在p.start()之前 p.join() ---> 主进程等子进程执行完 之后再结 ...

  5. django定义Model中的方法和属性

    #定义一个Model class UserProfile(models.Model): user=models.OneToOneField(User,unique=True) phone=models ...

  6. Spring之AOP在XML中的配置方法

    AOP 即 Aspect Oriental Program 面向切面编程 先来一个栗子: <aop:config> <aop:pointcut id="loggerCutp ...

  7. 全面理解Javascript中Function对象的属性和方法

    http://www.cnblogs.com/liontone/p/3970420.html 函数是 JavaScript 中的基本数据类型,在函数这个对象上定义了一些属性和方法,下面我们逐一来介绍这 ...

  8. Asp.net mvc 中Action 方法的执行(一)

    [toc] 在 Aps.net mvc 应用中对请求的处理最终都是转换为对某个 Controller 中的某个 Action 方法的调用,因此,要对一个请求进行处理,第一步,需要根据请求解析出对应的 ...

  9. window对象的方法和属性汇总

    window对象有以下方法: open close alert confirm prompt setTimeout clearTimeout setInterval clearInterval mov ...

随机推荐

  1. Trident中的解析包含的函数操作与投影操作

    一:函数操作 1.介绍 Tuple本身是不可变的 Function只是在原有的基础上追加新的tuple 2.说明 如果原来的字段是log,flag 新增之后的tuple可以访问这些字段,log,fla ...

  2. Extracted SQL state class 'S1' from value 'S1009'

    发现不查所有字段时是可以查询的,最后一个个字段尝试,发现是在passwd_time这个字段时有问题,然后看看这个时间 是无效的时间,改成有效时间即可.     相关链接: Mysql 时间 '0000 ...

  3. rem+media+jquery布局结局方案

    ; ; } ? ; + 'px'; } document.addEventListener('DOMContentLoaded', callback); window.addEventListener ...

  4. 第K人||约瑟夫环(链表)

    http://oj.acm.zstu.edu.cn/JudgeOnline/problem.php?id=4442 很容易超时 通过数组来记录,删除 //数组从1开始好像不行 后面一些数字就乱码了,因 ...

  5. 最佳linux文件WINDOWS上传下载方法

    通常,利用SSH管理远程Linux服务器时,经常需要与本地交互文件.当然,我们可以利用FTP方式,比如通过Filezilla客户端软件.不过直接使用SSH软件(SecureCRT.Xshell)自带的 ...

  6. VS2017动态链接库(.dll)的生成与使用

    转 https://blog.csdn.net/m0_37170593/article/details/76445972 这里以VS2017为例子,讲解一下动态链接库(.dll)的生成与使用. 一.动 ...

  7. HDU 5536 Chip Factory (暴力+01字典树)

    <题目链接> 题目大意: 给定一个数字序列,让你从中找出三个不同的数,从而求出:$\max_{i,j,k} (s_i+s_j) \oplus s_k$的值. 解题分析:先建好01字典树,然 ...

  8. asp.net core自定义模型验证——前端验证

    转载请注明出处:http://www.cnblogs.com/zhiyong-ITNote/ 官方网站:https://docs.microsoft.com/zh-cn/aspnet/core/mvc ...

  9. native和html5的通信方案

    一.jsbridge 重写WebView中WebChromeClient类的onJsPrompt()方法 二.url里面带参数 三.在js里面写全局函数,在native中调用

  10. 拓扑排序 --- AtCode - 3596

    题目链接: https://cn.vjudge.net/problem/1137733/origin 拓扑排序的基本思想: https://blog.csdn.net/qq_41713256/arti ...