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