c# 用反射怎么获取 类->类->属性

2025-01-18 18:01:37
推荐回答(1个)
回答1:

string file = AppDomain.CurrentDomain.BaseDirectory + "ConsoleTest.exe";

Assembly assembly = Assembly.LoadFile(file);
//参数: 命名空间+类名

Type type = assembly.GetType("ConsoleTest.ChangeFileName");

object obj = assembly.CreateInstance(type.FullName);
//方法名

MethodInfo info = type.GetMethod("ChangeName");
//方法参数

string path = "E:\\temp\\";

object[] objs = new object[] { path };

try
{
//调用方法

info.Invoke(obj, objs);
}
catch (Exception ex)
{
Console.Write(ex.Message);
}