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("\t\t\tmethodInfoitem:" + 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);

}

}
}

posted @ 2014-12-24 16:24  hbweixiang  阅读(131)  评论(0编辑  收藏  举报