Reflection in C# Language
Reflection is a powerful and advanced feature of the C# language that allows you to inspect, query, and inte
ract with the metadata of types and objects during runtime. It provides you with the ability to examine the structure of your code, access type information, and even create instances of types dynamically. In this post, we’ll explore the basics of reflection in C# and provide examples to illustrate its practical applications.Why Use Reflection?
Reflection can be incredibly useful in various scenarios, including:
- Plug-ins and Extensibility: You can load and interact with external assemblies dynamically, making it possible to build extensible applications.
- Serialization: You can automatically serialize and deserialize objects, as many serialization libraries use reflection to access object properties.
- Attribute-Based Programming: Reflection can be used to examine custom attributes applied to classes, methods, and properties, which can help in building custom frameworks.
- Testing and Debugging: Reflection can assist in writing unit tests and debugging tools.
- Code Generation: You can dynamically generate code and classes based on the existing types.
Key Classes for Reflection
In C#, reflection is primarily handled through the System.Reflection
namespace. Some key classes in this namespace include:
Type
: Represents a type, allowing you to access its methods, properties, fields, and attributes.Assembly
: Represents an assembly, which is a compiled collection of types.MethodInfo
,PropertyInfo
, andFieldInfo
: Provide information about methods, properties, and fields, respectively.Activator
: Allows you to create instances of types dynamically.
Example 1: Getting Type Information
Let’s say you have a class Person
:
class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
You can use reflection to get information about this class:
Type personType = typeof(Person);
Console.WriteLine("Type Name: " + personType.Name);
foreach (var prop in personType.GetProperties())
{
Console.WriteLine("Property: " + prop.Name);
}
Example 2: Creating an Object Dynamically
You can use reflection to create an instance of a type without knowing its type at compile time:
Type dynamicType = Type.GetType("YourNamespace.YourClass");
object instance = Activator.CreateInstance(dynamicType);
Example 3: Invoking Methods
You can invoke methods dynamically using reflection:
Type type = typeof(MyClass);
MethodInfo methodInfo = type.GetMethod("SomeMethod");
object instance = Activator.CreateInstance(type);
object result = methodInfo.Invoke(instance, null);
Example 4: Using Custom Attributes
Reflection is commonly used to inspect custom attributes applied to types or members. For instance:
[MyCustomAttribute("Sample Attribute")]
class MyClass
{
// ...
}
You can use reflection to access this attribute:
Type type = typeof(MyClass);
var attributes = type.GetCustomAttributes(typeof(MyCustomAttribute), false);
Caution and Performance
While reflection provides great flexibility, it should be used judiciously, as it can have performance overhead and may lead to runtime errors if not handled properly. Also, security concerns should be addressed when using reflection, especially when loading external assemblies dynamically.
Discover more from PiEmbSysTech
Subscribe to get the latest posts sent to your email.