# <img src="/package_icon.png" height="30px"> MethodDecorator.Fody
[![Chat on Gitter](https://img.shields.io/gitter/room/fody/fody.svg)](https://gitter.im/Fody/Fody)
[![NuGet Status](https://img.shields.io/nuget/v/MethodDecorator.Fody.svg)](https://www.nuget.org/packages/MethodDecorator.Fody/)
Compile time decorator pattern via IL rewriting.
### This is an add-in for [Fody](https://github.com/Fody/Home/)
**It is expected that all developers using Fody either [become a Patron on OpenCollective](https://opencollective.com/fody/contribute/patron-3059), or have a [Tidelift Subscription](https://tidelift.com/subscription/pkg/nuget-fody?utm_source=nuget-fody&utm_medium=referral&utm_campaign=enterprise). [See Licensing/Patron FAQ](https://github.com/Fody/Home/blob/master/pages/licensing-patron-faq.md) for more information.**
## Usage
See also [Fody usage](https://github.com/Fody/Home/blob/master/pages/usage.md).
### NuGet installation
Install the [MethodDecorator.Fody NuGet package](https://nuget.org/packages/MethodDecorator.Fody/) and update the [Fody NuGet package](https://nuget.org/packages/Fody/):
```powershell
PM> Install-Package Fody
PM> Install-Package MethodDecorator.Fody
```
The `Install-Package Fody` is required since NuGet always defaults to the oldest, and most buggy, version of any dependency.
### Your Code
```c#
// Attribute should be "registered" by adding as module or assembly custom attribute
[module: Interceptor]
// Any attribute which provides OnEntry/OnExit/OnException with proper args
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Assembly | AttributeTargets.Module)]
public class InterceptorAttribute : Attribute, IMethodDecorator {
// 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) {
TestMessages.Record(string.Format("Init: {0} [{1}]", method.DeclaringType.FullName + "." + method.Name, args.Length));
}
public void OnEntry() {
TestMessages.Record("OnEntry");
}
public void OnExit() {
TestMessages.Record("OnExit");
}
public void OnException(Exception exception) {
TestMessages.Record(string.Format("OnException: {0}: {1}", exception.GetType(), exception.Message));
}
}
public class Sample {
[Interceptor]
public void Method()
{
Debug.WriteLine("Your Code");
}
}
```
### What gets compiled
```c#
public class Sample {
public void Method(int value) {
InterceptorAttribute attribute =
(InterceptorAttribute) Activator.CreateInstance(typeof(InterceptorAttribute));
// in c# __methodref and __typeref don't exist, but you can create such IL
MethodBase method = MethodBase.GetMethodFromHandle(__methodref (Sample.Method), __typeref (Sample));
object[] args = new object[1] { (object) value };
attribute.Init((object)this, method, args);
attribute.OnEntry();
try {
Debug.WriteLine("Your Code");
attribute.OnExit();
}
catch (Exception exception) {
attribute.OnException(exception);
throw;
}
}
}
```
**NOTE:** *this* is replaced by *null* when the decorated method is static or a constructor.
### IntersectMethodsMarkedByAttribute
This is supposed to be used as
```c#
// all MSTest methods will be intersected by the code from IntersectMethodsMarkedBy
[module:IntersectMethodsMarkedBy(typeof(TestMethod))]
```
You can pass as many marker attributes to `IntersectMethodsMarkedBy` as you want
```c#
[module:IntersectMethodsMarkedBy(typeof(TestMethod), typeof(Fact), typeof(Obsolete))]
```
Example of `IntersectMethodsMarkedByAttribute` implementation
```c#
[AttributeUsage(AttributeTargets.Module | AttributeTargets.Assembly)]
public class IntersectMethodsMarkedByAttribute : Attribute {
// Required
public IntersectMethodsMarkedByAttribute() {}
public IntersectMethodsMarkedByAttribute(params Type[] types) {
if (types.All(x => typeof(Attribute).IsAssignableFrom(x))) {
throw new Exception("Meaningfull configuration exception");
}
}
public void Init(object instance, MethodBase method, object[] args) {}
public void OnEntry() {}
public void OnExit() {}
public void OnException(Exception exception) {}
// Optional
//public void OnTaskContinuation(Task task) {}
}
```
Now all your code marked by [TestMethodAttribute] will be intersected by IntersectMethodsMarkedByAttribute methods.
You can have multiple IntersectMethodsMarkedByAttributes applied if you want (don't have idea why).
MethodDecorator searches IntersectMethodsMarkedByAttribute by predicate StartsWith("IntersectMethodsMarkedByAttribute")
In case of exception in async method you "OnException" will not be called, OnTaskContinuation will be called instead.
## Icon
Icon courtesy of [The Noun Project](http://thenounproject.com)
没有合适的资源?快使用搜索试试~ 我知道了~
MethodDecorator:通过IL重写来编译时间装饰器模式
共118个文件
cs:100个
csproj:7个
yml:2个
需积分: 23 2 下载量 17 浏览量
2021-02-09
18:22:02
上传
评论
收藏 78KB ZIP 举报
温馨提示
MethodDecorator.Fody 通过IL重写来编译时间装饰器模式。 这是的加载项 期望所有使用开发人员要么,要么拥有。 更多信息,。 用法 另请参阅。 NuGet安装 安装并更新: PM > Install-Package Fody PM > Install-Package MethodDecorator.Fody 必须Install-Package Fody ,因为NuGet始终默认为任何依赖项的最早,也是漏洞最多的版本。 您的密码 // Attribute should be "registered" by adding as module or assembly custom attribute [ module : Interceptor ] // Any attribute which provides OnEntry/OnExit/OnException wit
资源详情
资源评论
资源推荐
收起资源包目录
MethodDecorator:通过IL重写来编译时间装饰器模式 (118个子文件)
MethodProcessor.cs 23KB
InterceptedMethods.cs 16KB
ModuleWeaver.cs 13KB
IlHelper.cs 6KB
WhenDecoratingVoidMethod.cs 6KB
WhenDecoratingPartial.cs 5KB
WhenDecoratingMethodsWithReturnValues.cs 4KB
WhenDecoratingFixed.cs 3KB
WhenDecoratingWithParameters.cs 3KB
InterceptingVoidMethods.cs 3KB
MethodEditContext.cs 3KB
InterceptingMethodsWithReturnValues.cs 2KB
TestsBase.cs 2KB
WhenDecoratingPropertyMethods.cs 2KB
InterceptorWithParamsAttribute.cs 1KB
TypeReferenceExtensions.cs 1KB
DerivedDecoratorAttribute.cs 1KB
ReferenceFinder.cs 1KB
TestRecords.cs 1KB
DerivedFromInterfaceDecoratorAttribute.cs 1KB
TestRecords.cs 1KB
DecoratingConstructors.cs 1KB
WhenDecoratingByExternalInterceptor.cs 1KB
StaticMembersDynamicWrapper.cs 1KB
WhenMatchingByRegex.cs 1KB
WhenInterceptingNestedTypes.cs 1KB
IPartialDecorator.cs 1KB
WhenDecoratedByDerivedMatchingAssembly.cs 1KB
WhenDecoratedByDerivedMatchingModule.cs 1KB
WhenMatchingByCommaSeparated.cs 1KB
SimpleTestBase.cs 1KB
WhenAsync.cs 1024B
MatchingDecoratorAttribute.cs 993B
InterceptorAttribute.cs 978B
DerivedMatchingDecoratorAttribute.cs 970B
WhenDecoratingGenericMethods.cs 952B
ExternalInterceptionAssemblyLevelAttribute.cs 894B
ExternalInterceptorAttribute.cs 879B
WhenDecoratedByDerivedMatchingModuleTypeExclude.cs 877B
WhenDecoratingByDerivedFromInterface.cs 875B
WhenDecoratingByDerivedInterceptor.cs 851B
IntersectMethodsMarkedByAttribute.cs 837B
InterceptorWithPriorityAttribute.cs 835B
InterceptorDerivedFromAbstractBaseClassAttribute.cs 797B
AssemblyInfo.cs 762B
MatchingByRegex.cs 717B
AsyncClass.cs 681B
ILProcessorExtensions.cs 659B
AssemblyExtensions.cs 656B
InterceptorBypassReturnAttribute.cs 652B
InterceptedClass.cs 646B
InterceptingNestedTypes.cs 613B
InterceptorExit1ExceptionAttribute.cs 603B
WhenDecoratingGenericTypes.cs 589B
InterceptorAlterRetvalAttribute.cs 577B
WhenDecoratingExtensionMethods.cs 573B
MethodDecoratorAttributeBase.cs 569B
AssemblyExtensions.cs 559B
InterceptingConstructors.cs 553B
NoInitMethodDecoratorAttribute.cs 552B
InterceptorBypassAttribute.cs 544B
InterceptingPropertyMethods.cs 537B
InterceptorInit3Attribute.cs 517B
MarkedFromTheDerivedInterface.cs 509B
InterceptingExtensionMethods.cs 507B
InterceptorInit2Attribute.cs 489B
WeaverHelperWrapper.cs 475B
DerivedMatchingModuleTypeExclude.cs 475B
MarkedFromTheDerivedDecorator.cs 472B
InterceptorExceptionAttribute.cs 469B
InterceptorInit1Attribute.cs 468B
MarkedFromAnotherAssembly.cs 461B
WeaverHelperWrapper.cs 446B
InterceptorExit1Attribute.cs 431B
MethodDecoratorAttribute.cs 415B
InterceptorEntryAttribute.cs 408B
DerivedMatchingAssembly.cs 407B
InterceptorExitAttribute.cs 405B
DerivedMatchingModule.cs 403B
MatchingCommaSeparated.cs 395B
Sample.cs 390B
IMethodDecorator.cs 378B
InterceptingAbstractMethods.cs 378B
AspectMatchingAttributeBase.cs 374B
WhenDecoratingAbstractMethods.cs 344B
WhenDecoratedInderectlly.cs 344B
WhenDecoratedByNoInit.cs 337B
SimpleTestBase.cs 326B
ClassTestsBase.cs 290B
IAspectMatchingRule.cs 280B
GenericMethod.cs 258B
GenericType.cs 242B
MarkedWithInderectAttribute.cs 235B
MarkedWithNoInit.cs 230B
Method.cs 195B
Method.cs 188B
Method.cs 130B
TestConfig.cs 82B
TestConfig.cs 82B
AssemblyInfo.cs 51B
共 118 条
- 1
- 2
帝哲
- 粉丝: 36
- 资源: 4669
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于LUT查找表方法的正弦信号产生器FPGA实现,包含testbench,包括程序,注释,操作步骤
- Screenshot_20240618_174113.jpg
- matlab画正余弦函数图的代码!!!!!
- 2_期末网店运营报告模版.pdf
- MyBatisCodeHelperPro 3.3.2-2322 2023.2-2024.1
- 基于Python的简单的学生成绩管理程序设计(课程设计)
- jdk-8u20-windows-x64安装版本-jdk-8u301-linux-x64解压版
- 植物大战僵尸杂交版 修改阳光和冷却
- html css js网页设计ntion-model-for-开发笔记
- 数据库课程设计-processing开发笔记
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0