C# Advanced Topics
Reflection
Reflection allows you to inspect and manipulate the metadata of assemblies, modules, and types at runtime. This is useful for dynamic loading of types, attribute-based programming, and creating generic host environments.
Key Concepts:
System.Reflection
namespaceType
class for inspecting typesMethodInfo
,PropertyInfo
,FieldInfo
for membersActivator.CreateInstance
for dynamic instantiation- Attributes and their usage with reflection
For more details, see the API Reference.
using System.Reflection;
// Example: Getting type information
Type myType = typeof(MyClass);
Console.WriteLine($"Type Name: {myType.Name}");
// Example: Invoking a method dynamically
MethodInfo method = myType.GetMethod("MyMethod");
object instance = Activator.CreateInstance(myType);
method.Invoke(instance, null);
Attributes
Attributes provide declarative information about code elements (classes, methods, properties, etc.). This metadata can be inspected at runtime using reflection.
Common Attributes:
[Serializable]
[Obsolete]
[Conditional]
- Custom attributes for specific framework requirements
Learn how to create custom attributes.
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class MyCustomAttribute : Attribute
{
public string Description { get; }
public MyCustomAttribute(string description)
{
Description = description;
}
}
[MyCustom("This is a sample class.")]
public class ExampleClass
{
[MyCustom("This is a sample method.")]
public void SampleMethod() { }
}
Expression Trees
Expression trees represent code as a data structure. They are crucial for building dynamic queries, such as those used in LINQ to SQL or Entity Framework.
Key Concepts:
System.Linq.Expressions
namespaceExpression
class and its subclasses (e.g.,MethodCallExpression
,BinaryExpression
)- Compiling expression trees into executable code
- Building expression trees programmatically
Explore API details.
using System.Linq.Expressions;
// Represents x => x + 1
ParameterExpression param = Expression.Parameter(typeof(int), "x");
Expression body = Expression.Add(param, Expression.Constant(1));
Expression> lambda = Expression.Lambda>(body, param);
Func compiled = lambda.Compile();
int result = compiled(5); // result is 6
Dynamic Language Runtime (DLR)
The DLR provides an infrastructure for integrating dynamic languages into the .NET Framework. It enables dynamic type checking, late binding, and interoperability between dynamic languages and .NET code.
Key Features:
System.Dynamic
namespaceIDynamicMetaObjectProvider
interface- Dynamic compilation and interpretation
- Interoperability with languages like Python and Ruby
Interop (P/Invoke and COM Interop)
These techniques allow managed code (C#) to interact with unmanaged code (like C++ libraries) and COM components.
P/Invoke (Platform Invoke):
Used to call native code from managed code.
using System.Runtime.InteropServices;
public class NativeMethods
{
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int MessageBox(
IntPtr hWnd,
String text,
String caption,
int options);
}
COM Interop:
Used to interact with COM objects.