c# - How do I wire up an IAspectProvider so that PostSharp will use it? -
i've written class in c# implements postsharp's iaspectprovider
interface. don't understand i'm supposed have it. can't seem find documentation on postsharp's site tells me class once it's written.
does postsharp automatically find class because it's derived iaspectprovider
, use it? or there link available page have far been unable find?
fwiw, class provided below. (methodtracingaspect
our custom aspect we're using training.)
namespace loggingsample { using system; using system.collections.generic; using system.linq; using postsharp.aspects; internal class provideraspect : iaspectprovider { public ienumerable<aspectinstance> provideaspects(object targetelement) { var type = (type) targetelement; return type.getmethods() .where(m => type.isassignablefrom(typeof(ireportbuilder)) && m.ispublic && m.name == "execute") .select(m => new aspectinstance(targetelement, new methodtracingaspect())); } } }
for postsharp, class implementing iaspectprovider
aspect , need apply target elements in same way other aspect. so, usually, aspect provider class should derive 1 of attribute classes. can apply attribute target element , element passed provideaspects
method during build time.
in specific example can derive typelevelaspect
, because expect targetelement
type.
an example of aspect provider available on documentation page: http://doc.postsharp.net/example-dataattributes
example of aspect usage:
// aspect class [serializable] public class provideraspect : typelevelaspect, iaspectprovider { public ienumerable<aspectinstance> provideaspects(object targetelement) { // ... } } // apply single type [provideraspect] public class targetclass : ireportbuilder { public void execute() { // ... } } // apply many types [assembly: provideraspect (attributetargettypes="ourcompany.ourapplication.reports.*")]
you can find more information applying aspect code on documentation page: http://doc.postsharp.net/applying-aspects
p.s. review our documentation page aspect providers , add information usage.
Comments
Post a Comment