c# - Change return value of the method with attribute -


i want have class (pseudocode):

public myclass {    private bool isvalid;     [checkvalid]    public int dosomething()    {       return 1;    } } 

now, want [checkvalid] attribute check isvalid property, , if true, allow dosomething() execute, or, if false, make dosomething() return default(int) (or @ least make code inside dosomething() inaccesible)

that not feature of c#: attributes not executed such. there are, however, extension frameworks postsharp would allow - rewrites code @ il level inject functionality, typically based on attributes. requires additional tooling.

in c# "out of box", best bet be:

public int dosomething() {     checkvalid(); // might throw exception     return 1; } 

Comments

Popular posts from this blog

c# - Attribute value in root node of xml Linq to XML -

ios swift - uiimage array to video disorted and flipped -

scala, transform a callback pattern to a functional style internal iterator -