System.Reflection
描述
包含用于在运行时检查和操作类型、方法、属性等的类,实现反射功能
参数
参数名 |
类型 |
描述 |
typeName |
string |
类型名称 |
示例
示例代码,使用反射创建对象:
using System.Reflection;
class Program {
static void Main() {
Type type = typeof(System.Text.StringBuilder);
object instance = Activator.CreateInstance(type);
MethodInfo appendMethod = type.GetMethod("Append", new[] { typeof(string) });
appendMethod.Invoke(instance, new object[] { "Hello, Reflection!" });
MethodInfo toStringMethod = type.GetMethod("ToString");
string result = (string)toStringMethod.Invoke(instance, null);
Console.WriteLine(result); // 输出: Hello, Reflection!
}
}