User Tools

Site Tools


notes:csharp:reflection

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
notes:csharp:reflection [2019/01/31]
leszek [Reflection in C#]
notes:csharp:reflection [2019/01/31] (current)
leszek [Attributes]
Line 465: Line 465:
  
  
- 
-====Attributes==== 
- 
-Attributes can be applied to: 
-  * assemblies 
-  * types 
-  * methods 
-  * parameters 
-  * properties 
- 
-//Attribute usage constraints//​ enforce correct application of a custom attribute using the //​AttributeUsage//​ attribute and the //​AttributeTargets//​ enum. In other words, they define the targets for a custom attribute. 
- 
-Guidelines for developing custom attributes: 
-  * A custom attribute class has to derive from //​System.Attribute//​ (directly or indirectly) 
-  * A custom attribute name should have a suffix "​-Attribute"​. 
-  * Properties of a custom attribute have to be read-write. 
- 
-Reflecting attributes: 
-  * Use the //​Type.GetCustomAttributes//​ method. 
-  * Use the static members of the //​Attribute//​ class such as //​Attribute.IsDefined//​. 
- 
-Examples of attribute definitions and permitted annotations:​ 
-<code csharp> 
-// Attribute definition. 
-public class TestAttribute : System.Attribute { } 
- 
-// Permitted annotation. 
-[Test] 
-</​code>​ 
- 
-<code csharp> 
-// Attribute definition that defines a positional parameter as a constructor parameter. 
-public class TestAttribute : System.Attribute 
-{ 
-    public TestAttribute(int order) 
-    { 
-        this.order = order; 
-    } 
-    ​ 
-    private int order; 
-} 
- 
-// Permitted annotations. 
-[Test(1)] 
-[Test(12)] etc. 
-</​code>​ 
- 
-<code csharp> 
-// Attribute definition that defines an optional named argument as a public read/write property: 
-public class TestAttribute : System.Attribute 
-{ 
-    public bool Descending {get; set;} 
-    ​ 
-    public TestAttribute(int order) 
-    { 
-        this.order = order; 
-    } 
-    ​ 
-    private int order; 
-} 
- 
-// Permitted annotations. 
-[Test(1, Descending = true)] 
-[Test(8)] etc. 
-</​code>​ 
- 
- 
- 
-Example: Define a custom attribute TestDescriptionAttribute and apply it to a class TestClass: 
-<code csharp> 
-[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct,​ AllowMultiple = true)] 
-public class TestDescriptionAttribute : System.Attribute 
-{ 
-    private string description;​ 
-    public string Description 
-    { 
-        get { return description;​ } 
-        set { description = value; } 
-    } 
- 
-    public TestDescriptionAttribute() { } 
- 
-    // This ctor takes a string as a parameter. As a result we are able to pass a string to 
-    // our attribute: [TestDescription("​string"​)] 
-    public TestDescriptionAttribute(string desc) 
-    { 
-        description = desc; 
-    } 
-} 
-... 
-[TestDescription("​This is class description."​)] 
-public class TestClass { } 
-</​code>​ 
- 
-Example: Check if a class TestClass has the //​Serializable//​ attribute applied: 
-<code csharp> 
-if (Attribute.IsDefined(typeof(TestClass),​ typeof(SerializableAttribute))) 
-{ 
-    // ... 
-} 
-... 
-[Serializable] 
-class TestClass { } 
-</​code>​ 
- 
-Example: Obtain all attributes applied to the TestClass that are of type TestAttribute:​ 
-<code csharp> 
-IEnumerable<​CustomAttributeData>​ attrs = typeof(TestClass).GetType().GetCustomAttributes(TestAttribute);​ 
-... 
-[Test] 
-class TestClass { } 
-</​code>​ 
-        ​ 
-Example: Obtain an instance of ConditionalAttribute and examine its ConditionString property: 
-<code csharp> 
-MethodInfo info = typeof(TestClass).GetMethod("​TestMethod"​);​ 
- 
-if (Attribute.IsDefined(info,​ typeof(ConditionalAttribute))) 
-{ 
-    ConditionalAttribute attr = 
-        (ConditionalAttribute)Attribute.GetCustomAttribute(info,​ typeof(ConditionalAttribute));​ 
- 
-    if (attr != null) 
-        Console.WriteLine(attr.ConditionString);​ // DEBUG 
-} 
-... 
-class TestClass 
-{ 
-    [Conditional("​DEBUG"​)] 
-    public void TestMethod(string s) { } 
-} 
-</​code>​ 
- 
-Example: Obtain assembly-level attributes //​AssemblyCompanyAttribute//​ and //​AssemblyTitleAttribute//:​ 
-<code csharp> 
-string assemblyCompany = 
-    ((AssemblyCompanyAttribute)Attribute.GetCustomAttribute(Assembly.GetExecutingAssembly(),​ 
-        typeof(AssemblyCompanyAttribute),​ false)).Company;​ 
-string assemblyTitle = 
-    ((AssemblyTitleAttribute)Attribute.GetCustomAttribute(Assembly.GetExecutingAssembly(),​ 
-        typeof(AssemblyTitleAttribute),​ false)).Title;​ 
-</​code>​ 
  
  
notes/csharp/reflection.txt ยท Last modified: 2019/01/31 by leszek