C# Reflection is a mechanism that enables inspection and manipulation of classes, interfaces, methods, properties, and other types at runtime. It allows developers to dynamically load assemblies, create instances of classes, invoke methods, and access properties dynamically.
Using reflection in C#, you can:
– Retrieve information about types: You can get information about the properties, fields, methods, events, and constructors of a type using reflection. You can also access metadata attributes defined on types.
– Create instances of types: You can use reflection to create an instance of a class, even if you don’t know the class name at compile time.
– Invoke methods dynamically: Reflection allows you to invoke methods dynamically at runtime. You can get a MethodInfo object representing a method and invoke it using the Invoke method.
– Access properties and fields: Reflection provides ways to get or set the values of properties and fields dynamically. You can use the PropertyInfo and FieldInfo classes to access properties and fields, respectively.
– Retrieve and apply attributes: Reflection enables you to retrieve attributes defined on types, methods, properties, and other members. You can use the Attribute class and its derived classes to define and apply custom attributes to your code.
Attributes, on the other hand, are declarative tags that you can apply to your code elements such as classes, methods, properties, etc. Attributes provide additional information about the code element.
In C#, attributes are defined by deriving from the Attribute class. You can apply attributes using square brackets [] just before the code element you want to annotate. For example:
“`
[Serializable]
public class MyClass { }
“`
In the above example, the Serializable attribute is applied to the MyClass class. This attribute indicates that instances of the class can be serialized.
You can also define your own custom attributes by creating a class that inherits from Attribute. To use the custom attribute, you apply it to the desired code element using the square brackets [].
Reflection can be used to retrieve attributes applied to code elements. You can use various methods and properties provided by the reflection API to discover and access attributes. For example, the GetCustomAttributes method can be used to retrieve all attributes applied to a code element.
Overall, reflection and attributes are powerful features of C# that enable dynamic behavior and metadata-driven programming. They provide flexibility and extensibility to your code by enabling runtime inspection and manipulation of types and their metadata.