Reflection in C#
Reflection is a mechanism in C# that allows a program to inspect and manipulate the metadata of types and objects at runtime. It can be used to create objects, invoke methods, get and set property values, etc. Here's an example code that demonstrates how to use reflection to create an object and invoke a method:
class MyClass
{
public void MyMethod(string arg)
{
Console.WriteLine("MyMethod called with argument {0}.", arg);
}
}
Type type = typeof(MyClass);
object obj = Activator.CreateInstance(type);
MethodInfo method = type.GetMethod("MyMethod");
method.Invoke(obj, new object[] { "test" }); //
No comments:
Post a Comment