问题
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