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# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -