【发布时间】:2011-01-27 08:34:39
【问题描述】:
我正在加载运行时程序集,但我不知道任何类或方法的名称。我不想列出我的程序集中的所有类及其声明的方法,而不是从 System.Object 继承的那些。
这是代码:
string str = "";
Assembly assembly = Assembly.LoadFile(@"c:\components.dll");
foreach (Type type in assembly.GetTypes())
{
if (type.IsClass == true)
{
str += type.Name + "\n";
MethodInfo[] methodInfo = type.GetMethods(BindingFlags.DeclaredOnly);
foreach (MethodInfo mi in methodInfo)
{
str += "\t" + mi.Name + "\n";
}
}
}
MessageBox.Show(str);
这是 components.dll:
public class component01
{
public string myName = "component01";
public string getMyName()
{
return myName;
}
}
public class component02
{
public string myName = "component02";
public string getMyName()
{
return myName;
}
}
结果:
component01
component02
And if i remove the bindingflag:
component01
getMyName
ToString
Equals
GetHashcode
GetType
component02
getMyName
ToString
Equals
GetHashcode
GetType
我只想显示getMyName 方法。
【问题讨论】:
标签: c# .net reflection assemblies load