C # use custom Attribute

Source: Internet
Author: User

C # use custom Attribute

C # features are frequently used in programming,Metadata equivalent to class

Custom feature inheritanceSystem. AttributeClass

Custom feature nameSuffix: AttributeIn line with Microsoft's naming style and compiler's Search rules.

Use [] syntax to use custom features

You can use reflection to view custom features.


[AttributeUsage (AttributeTargets. class, AllowMultiple = true, Inherited = false)] public class CTest2Attribute: Attribute {private string _ strTableName = string. empty; public string TableName {get {return (_ strTableName) ;}set {_ strTableName = value ;}} private string _ fieldName = string. empty; public string FieldName {get {return (_ fieldName) ;}set {_ fieldName = value ;}} [CTest2 (TableName = "test table 1 ", fieldName = "Field 1")] public class TestAtt2 {public string Temp {get; set ;}}

object[] attrs = typeof(TestAtt2).GetCustomAttributes(true);foreach (object obj in attrs){    CTest2Attribute att = obj as CTest2Attribute;    if (att != null)    {        Console.WriteLine(att.TableName);    }}


Related Article

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.