c# - Is there a way to apply an attribute to a method that executes first? -


without using library postsharp, there way set custom attribute can have logic in when attached method, execute prior entering method?

no; attributed not intended inject code. tools postsharp around smoke , mirrors, without that: no. option might decorator pattern, perhaps dynamically implementing interface (not trivial means). however, adding utility method-call top of method(s) much simpler, , presumably fine since if have access add attributes have access add method-call.

or put way: tools postsharp exist precicely because doesn't exist out-of-the-box.

// poor man's aspect oriented programming public void foo() {     someutility.dosomething();     // real code } 

in cases, subclassing may useful, if subclass done @ runtime (meta-programming):

class youwritethisatruntimewithtypebuilder : yourtype {     public override void foo() {         someutility.dosomething();         base.foo();     } } 

Comments

Popular posts from this blog

jasper reports - Fixed header in Excel using JasperReports -

media player - Android: mediaplayer went away with unhandled events -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -