Overview
The MethodInfo
class represents a method and provides information about its characteristics, as well as a way to invoke the method dynamically. In the context of System.Reflection.Emit
, it is used in type generation scenarios to examine method metadata while emitting IL.
Namespace
System.Reflection.Emit
Assembly
System.Reflection.Emit.dll
Syntax
public abstract class MethodInfo : MethodBase
{
// Properties
public abstract MethodAttributes Attributes { get; }
public abstract CallingConventions CallingConvention { get; }
// Methods
public abstract ParameterInfo[] GetParameters();
public abstract MethodInfo MakeGenericMethod(params Type[] typeArguments);
public abstract Type[] GetGenericArguments();
public abstract bool IsGenericMethod { get; }
// ... Additional members omitted for brevity
}
Members
▶ Properties
Attributes
– Gets the attributes for this method.CallingConvention
– Gets the calling convention of the method.IsGenericMethod
– Indicates whether the method is generic.IsGenericMethodDefinition
– Indicates whether the method is a generic method definition.MetadataToken
– Gets a handle to the metadata for this method.
▶ Methods
GetParameters()
– Returns an array ofParameterInfo
objects.MakeGenericMethod(Type[] typeArguments)
– Creates a constructed generic method.GetGenericArguments()
– Returns the generic type arguments of the method.Invoke(object obj, BindingFlags invokeAttr, Binder binder, object[] parameters, CultureInfo culture)
– Invokes the method with the specified parameters.
Example
The following example demonstrates how to emit a dynamic type with a method, retrieve its MethodInfo
, and invoke it at runtime.
using System;
using System.Reflection;
using System.Reflection.Emit;
public class EmitDemo
{
public static void Main()
{
// Define a dynamic assembly and module
AssemblyName asmName = new AssemblyName("DynamicAsm");
AssemblyBuilder asmBuilder = AssemblyBuilder.DefineDynamicAssembly(asmName, AssemblyBuilderAccess.Run);
ModuleBuilder modBuilder = asmBuilder.DefineDynamicModule("MainModule");
// Define a public class named 'HelloWorld'
TypeBuilder typeBuilder = modBuilder.DefineType("HelloWorld", TypeAttributes.Public);
// Define a public method 'SayHello' that returns void and takes a string argument
MethodBuilder methodBuilder = typeBuilder.DefineMethod(
"SayHello",
MethodAttributes.Public,
typeof(void),
new Type[] { typeof(string) });
// Generate IL for the method
ILGenerator il = methodBuilder.GetILGenerator();
il.Emit(OpCodes.Ldstr, "Hello, {0}!");
il.Emit(OpCodes.Ldarg_1);
MethodInfo writeLine = typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string), typeof(object) });
il.Emit(OpCodes.Call, writeLine);
il.Emit(OpCodes.Ret);
// Create the type
Type helloType = typeBuilder.CreateType();
// Get the MethodInfo for SayHello
MethodInfo mi = helloType.GetMethod("SayHello");
// Create an instance and invoke the method
object instance = Activator.CreateInstance(helloType);
mi.Invoke(instance, new object[] { "World" });
}
}
Output:
Hello, World!
Remarks
MethodInfo
is the base class for method reflection. When working with System.Reflection.Emit
, you typically obtain a MethodInfo
from a MethodBuilder
after the type has been created. The MethodInfo
can then be used for further introspection or dynamic invocation.