. NET Attribute Getting Started "notes"

Source: Internet
Author: User

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"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.