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
Post a Comment