The fact that you can define custom attributes and place them in your source code would be of little value without some way of retrieving that information and acting on it. By using reflection, you can retrieve the information that was defined with custom attributes. The key method is GetCustomAttributes, which returns an array of objects that are the run-time equivalents of the source code attributes. This method has several overloaded versions. For more information, see Attribute.
An attribute specification such as:
[Author("P. Ackerman", version =1.1)] classSampleClass
Why should I use it?
Reflection is a C # language mechanism for accessing dynamic properties of objects at run time. Reflection is typically used to obtain information about the dynamic object type and the object attribute values. In REST application, for example, reflection could be used to iterate through the serialized response object.
Observations
Reflection allows code to access information about assemblies, modules, and types at run time (program execution). This can then be used to dynamically create, modify, or access types. The types include properties, methods, fields, and attributes.
usingSystem;usingSystem.Linq;usingSystem.Reflection;namespaceReflection{classProgram {staticvoidMain(string[] args) {var assembly =Assembly.GetExecutingAssembly(); Console.WriteLine(assembly.FullName);var types =assembly.GetTypes();foreach (var type in types) {Console.WriteLine($"The type name is: {type.Name}");var props =type.GetProperties();foreach (var prop in props) {Console.WriteLine($"\tThe property is: {prop.Name} Property Type: {prop.PropertyType}"); }var fields =type.GetFields();foreach (var field in fields) {Console.WriteLine($"\tThe field is: {field.Name} Field Type: {field.FieldType}"); }var methods =type.GetMethods();foreach (var method in methods) {Console.WriteLine($"The method is: {method.Name}"); } }var sample =newSample { Name ="Albr", Age =30 };var sampleType =typeof(Sample);var nameProperty =sampleType.GetProperty("Name");if (nameProperty !=null) {Console.WriteLine($"Property name: {nameProperty.Name}");Console.WriteLine($"Property value: {nameProperty.GetValue(sample)}"); }var myMethod =sampleType.GetMethod("MyMethod");myMethod.Invoke(sample,null);Console.WriteLine("==========================");var attributeTypes =assembly.GetTypes().Where(t =>t.GetCustomAttributes<MyClassAttribute>().Count() >0);foreach (var type in attributeTypes) {Console.WriteLine($"The type is: {type.Name}");var methodsCustomAttributes =type.GetMethods().Where(m =>m.GetCustomAttributes<MyMethodAttribute>().Count() >0);foreach (var method in methodsCustomAttributes) {Console.WriteLine($"The method name is: {method.Name}"); } }Console.ReadLine(); } } [MyClass]publicclassSample {publicstring Name { get; set; } // This is a propertypublicint Age; // this is a field [MyMethod]publicvoidMyMethod() {Console.WriteLine("Hello from method"); } [MyMethod]publicvoidMyMethod2() { } } [AttributeUsage(AttributeTargets.Class)]publicclassMyClassAttribute:Attribute { } [AttributeUsage(AttributeTargets.Method)]publicclassMyMethodAttribute:Attribute { }}