PostSharp - How to inject a method into a class?

寵の児 提交于 2020-01-02 09:38:08

问题


I have some problem with PostSharp (I assume that problem can be solved by other library). I have that scenario:

I must get all classes with name containing a word, e.g "Sth". I have that classes in my project (an example):

SthClass1.cs
SthClassBlabla.cs
SthClass3.cs

and I find that classes using PostSharp:

[assembly: TraceAttribute( AttributeTargetMembers = "Sth*")]

then, is it possible to inject (using aspect, PostSharp (?)) code for each found class ?

example:

 SthClass1 myObj = new SthClass1();
 myObj.ClassMethod1();
 myObj.ClassMethod2();
 myObj.InjectedMethod(); //that method belongs to the aspect

回答1:


Yes, you use introduction.

http://www.sharpcrafters.com/blog/post/PostSharp-Principals-Day-14-e28093-Introducing-members-and-interfaces-Part-1.aspx

http://www.sharpcrafters.com/blog/post/PostSharp-Principals-Day-15-e28093-Introducing-members-and-interfaces-Part-2.aspx



来源:https://stackoverflow.com/questions/8345318/postsharp-how-to-inject-a-method-into-a-class

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