C#中使用反射显示程序集的所有类型和属性(转)

本文介绍了一个C#应用程序如何加载并解析DLL文件中的类型、方法及属性。通过按钮点击事件触发,程序读取指定DLL文件,列出所有公共类型、方法数量及名称,并进一步获取非公开属性的数量。

///
///
/// loads a *.dll file from txtMethods and invokes all methods int it.
/// lists all types in the assembly
///
///
///
///
private void btnList_Click(object sender, System.EventArgs e)
{

string fileName=labelFile.Text.Trim();
string result="";
txtMethods.Text="";
txtTypes.Text="";

if(File.Exists(fileName))
{
try
{
Assembly assembly=Assembly.LoadFrom(fileName);
Type[] types=assembly.GetTypes();

result="The Assembly contains the following types :"+Environment.NewLine;

for(int i=0;i {
result+=" "+i+":"+types[i].Name+" "+" "+Environment.NewLine;

// Get the public methods.
MethodInfo[] myArrayMethodInfo=types[i].GetMethods(BindingFlags.Public|BindingFlags.Instance|BindingFlags.DeclaredOnly); txtMethods.Text=txtMethods.Text+Environment.NewLine+"The number of public methods in "+types[i].Name+" is "+myArrayMethodInfo.Length+Environment.NewLine;
// get all the methods.
txtMethods.Text=txtMethods.Text+getMethodInfo(myArrayMethodInfo);
/*
// Get the nonpublic methods.
MethodInfo[] myArrayMethodInfo1 = myType.GetMethods(BindingFlags.NonPublic|BindingFlags.Instance|BindingFlags.DeclaredOnly);
Console.WriteLine(" The number of protected methods is {0}.", myArrayMethodInfo1.Length);
// Display information for all methods.
LabelFile.Text=DisplayMethodInfo(myArrayMethodInfo1);
*/

}

foreach(Type myType in types)
{
// Get the public properties.
PropertyInfo[] myPropertyInfo = myType.GetProperties(BindingFlags.Public|BindingFlags.Instance);
Console.WriteLine("The mumber of public properties in "+myType.Name+" is {0}.", myPropertyInfo.Length);
// Display the public properties.
getPropertyInfo(myPropertyInfo);
// Get the nonpublic properties.
PropertyInfo[] myPropertyInfo1 = myType.GetProperties(BindingFlags.NonPublic|BindingFlags.Instance);
txtMethods.Text=txtMethods.Text+Environment.NewLine+("The number of NonPublic properties in "+myType.Name+" is "+ myPropertyInfo1.Length)+Environment.NewLine;
// Display all the nonpublic properties.
txtMethods.Text=txtMethods.Text+getPropertyInfo(myPropertyInfo1);
}

txtTypes.Text=result;
}
catch(Exception ee)
{
throw ee;
}

}

}

///
/// get Method informations from MethodInfo[] Array:
///
///
///
public string getMethodInfo(MethodInfo[] myArrayMethodInfo)
{
string methodStr="";
///
///getinformation for all methods.
for(int i=0;i {
MethodInfo myMethodInfo = (MethodInfo)myArrayMethodInfo[i];
methodStr+="Method "+i+" :"+ myMethodInfo.Name+Environment.NewLine;
}
return methodStr;
}

///
/// get properties information from PropertyInfo[] Array:
///
///
///
public string getPropertyInfo(PropertyInfo[] myPropertyInfo)
{
string propStr="";
// Display information for all properties.
for(int i=0;i {
PropertyInfo myPropInfo = (PropertyInfo)myPropertyInfo[i];
propStr+="property "+i+":"+ myPropInfo.Name+" type:"+ myPropInfo.PropertyType+Environment.NewLine;
}
return propStr;
}

[@more@]

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/8781179/viewspace-924752/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/8781179/viewspace-924752/

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值