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); }}