Created
April 8, 2014 11:28
-
-
Save lmatt-bit/10111810 to your computer and use it in GitHub Desktop.
Accessing Attributes by Using Reflection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Multiuse attribute. | |
| [System.AttributeUsage(System.AttributeTargets.Class | | |
| System.AttributeTargets.Struct, | |
| AllowMultiple = true) // Multiuse attribute. | |
| ] | |
| public class Author : System.Attribute | |
| { | |
| string name; | |
| public double version; | |
| public Author(string name) | |
| { | |
| this.name = name; | |
| // Default value. | |
| version = 1.0; | |
| } | |
| public string GetName() | |
| { | |
| return name; | |
| } | |
| } | |
| // Class with the Author attribute. | |
| [Author("P. Ackerman")] | |
| public class FirstClass | |
| { | |
| // ... | |
| } | |
| // Class without the Author attribute. | |
| public class SecondClass | |
| { | |
| // ... | |
| } | |
| // Class with multiple Author attributes. | |
| [Author("P. Ackerman"), Author("R. Koch", version = 2.0)] | |
| public class ThirdClass | |
| { | |
| // ... | |
| } | |
| class TestAuthorAttribute | |
| { | |
| static void Test() | |
| { | |
| PrintAuthorInfo(typeof(FirstClass)); | |
| PrintAuthorInfo(typeof(SecondClass)); | |
| PrintAuthorInfo(typeof(ThirdClass)); | |
| } | |
| private static void PrintAuthorInfo(System.Type t) | |
| { | |
| System.Console.WriteLine("Author information for {0}", t); | |
| // Using reflection. | |
| System.Attribute[] attrs = System.Attribute.GetCustomAttributes(t); // Reflection. | |
| // Displaying output. | |
| foreach (System.Attribute attr in attrs) | |
| { | |
| if (attr is Author) | |
| { | |
| Author a = (Author)attr; | |
| System.Console.WriteLine(" {0}, version {1:f}", a.GetName(), a.version); | |
| } | |
| } | |
| } | |
| } | |
| /* Output: | |
| Author information for FirstClass | |
| P. Ackerman, version 1.00 | |
| Author information for SecondClass | |
| Author information for ThirdClass | |
| R. Koch, version 2.00 | |
| P. Ackerman, version 1.00 | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment