Chat: There is no systematic study, occasionally look, see is not very clear.
But always do not understand, it is not difficult to write an example of their own hands. Results at a glance ...
attribute--Mark
You can attach to transactions such as methods, classes, and so on. Add properties.
Here are the tags I've seen most (don't say serializable here) Use cases--permissions
- Declare yourself a myattribut class--The existence of a field (Role), a constructor
- Write yourself a method myaction ()(or Class)--give it a tag you write, call through the constructor, and pass the argument
- Get a value to the currently logged-on user type role(Example: "1")
- Gets the markup for the myaction () method attributes
- Validation: Determines whether the current user type value (role) is equal to (or greater than)attributes. Role
1 class Program2 {3 Static voidMain (string[] args)4 {5 //gets the token. 6 //gets the token for the class. typeof (Person). GetCustomAttributes (typeof (Myfirstattribute), true);7 varattributes =typeof(program). GetMethod ("myaction"). GetCustomAttributes (typeof(MyAttribute),true);8MyAttribute myattribute = attributes[0] asMyAttribute;9 TenConsole.WriteLine ("If you get the current user name = token, it means that the method can be accessed. "); OneConsole.WriteLine ("Please enteryour user name level"); A stringSS =console.readline (); - - if(ss = =myattribute.role) the { - Console.WriteLine (Myaction ()); - } - Else + { -Console.WriteLine ("Insufficient authority. "); + } A } at /// <summary> - ///You can access me with a user type of 1. - /// </summary> - /// <returns></returns> -[My ("1")] - Public Static stringmyaction () in { - return "method can be accessed. "; to } + } - /// <summary> the ///the tags that you define yourself. "Permission Tag" * /// </summary> $ Public classMyattribute:attributePanax Notoginseng { - Public stringRole the { + Get; A Set; the } + PublicMyAttribute (stringrole) - { $ This. Role =role; $ } -}
. NET Attribute Getting Started "notes"