C#反射

Assembly assembly = Assembly.Load("project");
Assembly assembly2 = Assembly.LoadFrom("project.dll");

Module[] module = assembly.GetModules();
foreach (Module item in module)
{
Console.WriteLine("module:" + item);

}

//获取namespace,class

Type[] types = assembly.GetTypes();

foreach (Type item in types)
{
Console.WriteLine("type:" + item);
Type t = item;
MethodInfo[] methods = t.GetMethods();
IEnumerable<MethodInfo> methodInfo = t.GetRuntimeMethods();
foreach (MethodInfo methodInfoitem in methodInfo)
{
Console.WriteLine(" methodInfoitem:" + methodInfoitem);
}
Console.WriteLine();
foreach (var items in t.BaseType.GetMembers())
{
Console.WriteLine("BaseType:" + items);
}
foreach (MethodInfo method in methods)
{
Console.WriteLine("方法:" + method);
Type typeopen = assembly.GetType(Convert.ToString(item));
var user12 = Activator.CreateInstance(typeopen);
string chuan = Convert.ToString(method);
string strs = chuan.Substring(chuan.IndexOf(" ") + 1, chuan.Length - chuan.IndexOf(" ") - 1);
strs = strs.Substring(0, strs.IndexOf("("));
if (strs == "PublicShow")
{

typeopen.InvokeMember(strs,
BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance, null, user12,
null);

}

}
}

原文地址:https://www.cnblogs.com/david_king/p/4182727.html