Reflection Class
Namespace: System.Reflection.Emit
Assembly: System.Reflection.Emit.dll
Overview
Syntax
Members
Examples
The Reflection
class provides a set of static helper methods that simplify the creation of dynamic types using the Reflection.Emit API. It encapsulates common patterns for defining methods, fields, properties, and constructors at runtime.
Applies To
- .NET 5.0 and later
- Compatible with .NET Framework 4.6.1+
- .NET Core and .NET 6+
public static class Reflection
{
public static TypeBuilder DefineType(ModuleBuilder module,
string name,
TypeAttributes attributes = TypeAttributes.Public);
public static MethodBuilder DefineMethod(TypeBuilder typeBuilder,
string name,
MethodAttributes attributes,
Type returnType,
Type[] parameterTypes);
public static PropertyBuilder DefineProperty(TypeBuilder typeBuilder,
string name,
PropertyAttributes attributes,
Type propertyType,
Type[] parameterTypes);
// ...additional helper methods
}
Methods
Method | Returns | Description |
---|---|---|
DefineType | TypeBuilder | Creates a new TypeBuilder within the specified module. |
DefineMethod | MethodBuilder | Defines a method on a dynamic type. |
DefineProperty | PropertyBuilder | Defines a property on a dynamic type. |
DefineField | FieldBuilder | Defines a field on a dynamic type. |
DefineConstructor | ConstructorBuilder | Defines a constructor for a dynamic type. |
Example: Creating a Simple Dynamic Type
using System;
using System.Reflection;
using System.Reflection.Emit;
public class Demo
{
public static void Main()
{
AssemblyName asmName = new AssemblyName("DynamicAssembly");
AssemblyBuilder asmBuilder = AssemblyBuilder.DefineDynamicAssembly(asmName, AssemblyBuilderAccess.Run);
ModuleBuilder modBuilder = asmBuilder.DefineDynamicModule("MainModule");
// Use Reflection helper to define a new type
TypeBuilder tb = Reflection.DefineType(modBuilder, "Person");
// Define a private field: string _name;
FieldBuilder nameField = tb.DefineField("_name", typeof(string), FieldAttributes.Private);
// Define a public property: string Name { get; set; }
PropertyBuilder prop = Reflection.DefineProperty(tb, "Name",
PropertyAttributes.HasDefault,
typeof(string),
Type.EmptyTypes);
// Define the getter method
MethodBuilder getMethod = Reflection.DefineMethod(tb, "get_Name",
MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig,
typeof(string),
Type.EmptyTypes);
ILGenerator getIl = getMethod.GetILGenerator();
getIl.Emit(OpCodes.Ldarg_0);
getIl.Emit(OpCodes.Ldfld, nameField);
getIl.Emit(OpCodes.Ret);
prop.SetGetMethod(getMethod);
// Define the setter method
MethodBuilder setMethod = Reflection.DefineMethod(tb, "set_Name",
MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig,
null,
new Type[] { typeof(string) });
ILGenerator setIl = setMethod.GetILGenerator();
setIl.Emit(OpCodes.Ldarg_0);
setIl.Emit(OpCodes.Ldarg_1);
setIl.Emit(OpCodes.Stfld, nameField);
setIl.Emit(OpCodes.Ret);
prop.SetSetMethod(setMethod);
// Create the type
Type personType = tb.CreateType();
// Instantiate and use the dynamic type
object person = Activator.CreateInstance(personType);
personType.GetProperty("Name").SetValue(person, "Alice");
Console.WriteLine(personType.GetProperty("Name").GetValue(person));
}
}
Running the above program prints Alice
to the console, demonstrating how the Reflection
helper simplifies emitting a full type with a backing field and property.