🔮 Deep Dive: Reflection lets you inspect and manipulate types, methods, and properties at runtime. Powerful but use wisely!
Need to work with unknown types at runtime? Reflection is your tool.
🚀 Basic Reflection
// ✅ Get type information
Type type = typeof(MyClass);
Console.WriteLine($"Type: {type.Name}");
Console.WriteLine($"Namespace: {type.Namespace}");
Console.WriteLine($"Assembly: {type.Assembly}");
Console.WriteLine($"IsClass: {type.IsClass}");
Console.WriteLine($"IsAbstract: {type.IsAbstract}");
Console.WriteLine($"IsInterface: {type.IsInterface}");
// ✅ Get members
var properties = type.GetProperties();
var methods = type.GetMethods();
var fields = type.GetFields();
var constructors = type.GetConstructors();
// ✅ Create instance
object instance = Activator.CreateInstance(type);
// ✅ Invoke method
MethodInfo method = type.GetMethod("MyMethod");
method.Invoke(instance, new object[] { "parameter" });
💡 Real-World: Plugin System
// ✅ Plugin system with reflection
public interface IPlugin
{
string Name { get; }
void Execute();
}
public class PluginManager
{
private readonly List<IPlugin> _plugins = new();
public void LoadPlugins(string directory)
{
foreach (var dll in Directory.GetFiles(directory, "*.dll"))
{
// ✅ Load assembly
var assembly = Assembly.LoadFrom(dll);
// ✅ Find types implementing IPlugin
var pluginTypes = assembly.GetTypes()
.Where(t => typeof(IPlugin).IsAssignableFrom(t) && !t.IsAbstract);
foreach (var type in pluginTypes)
{
// ✅ Create instance
var plugin = (IPlugin)Activator.CreateInstance(type);
_plugins.Add(plugin);
Console.WriteLine($"Loaded plugin: {plugin.Name}");
}
}
}
public void ExecuteAll()
{
foreach (var plugin in _plugins)
{
plugin.Execute();
}
}
}
// ✅ Attribute-based discovery
[AttributeUsage(AttributeTargets.Class)]
public class PluginAttribute : Attribute
{
public string Name { get; }
public PluginAttribute(string name) => Name = name;
}
// ✅ Find plugins by attribute
var pluginTypes = assembly.GetTypes()
.Where(t => t.GetCustomAttribute<PluginAttribute>() != null);
💡 Expert Tip: Performance Optimization
// ❌ Slow: Reflection each time
for (int i = 0; i < 100000; i++)
{
var prop = typeof(MyClass).GetProperty("Name");
prop.SetValue(obj, "John");
}
// ✅ Fast: Cache reflection results
private static readonly PropertyInfo _nameProperty =
typeof(MyClass).GetProperty("Name");
private static readonly MethodInfo _calculateMethod =
typeof(MyClass).GetMethod("Calculate");
// ✅ Even faster: Delegate creation
private static readonly Func<MyClass, string, string> _setNameDelegate =
(Func<MyClass, string, string>)Delegate.CreateDelegate(
typeof(Func<MyClass, string, string>),
_nameProperty.GetSetMethod()
);
// ✅ Fastest: Expression trees
private static readonly Action<MyClass, string> _setNameAction =
(Action<MyClass, string>)CreateSetterDelegate(typeof(MyClass), "Name");
private static Delegate CreateSetterDelegate(Type type, string propertyName)
{
var param = Expression.Parameter(type, "instance");
var value = Expression.Parameter(typeof(string), "value");
var property = Expression.Property(param, propertyName);
var assign = Expression.Assign(property, value);
var lambda = Expression.Lambda(assign, param, value);
return lambda.Compile();
}


