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

///

1<summary>   
2///   
3/// loads a *.dll file from txtMethods and invokes all methods int it.   
4/// lists all types in the assembly   
5///   
6/// </summary>

///

1<param name="sender"/>

///

1<param name="e"/>

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

 1<types.getlength(0);++i) "+="" "+"="" "+environment.newline;="" "+myarraymethodinfo.length+environment.newline;="" "+mytype.name+"="" "+types[i].name+"="" *="" +i+":"+="" <summary="" all="" catch(exception="" console.writeline("\nthe="" console.writeline("the="" display="" ee)="" ee;="" for="" foreach(type="" get="" getpropertyinfo(mypropertyinfo);="" in="" information="" is="" labelfile.text="DisplayMethodInfo(myArrayMethodInfo1);" methodinfo[]="" methods="" methods.="" mumber="" myarraymethodinfo="types[i].GetMethods(BindingFlags.Public|BindingFlags.Instance|BindingFlags.DeclaredOnly);" myarraymethodinfo1="myType.GetMethods(BindingFlags.NonPublic|BindingFlags.Instance|BindingFlags.DeclaredOnly);" myarraymethodinfo1.length);="" mypropertyinfo="myType.GetProperties(BindingFlags.Public|BindingFlags.Instance);" mypropertyinfo.length);="" mypropertyinfo1="myType.GetProperties(BindingFlags.NonPublic|BindingFlags.Instance);" mypropertyinfo1.length)+environment.newline;="" mytype="" name+"="" nonpublic="" number="" of="" properties="" properties.="" propertyinfo[]="" protected="" public="" result+="\t " the="" throw="" txtmethods.text="txtMethods.Text+getPropertyInfo(myPropertyInfo1);" txttypes.text="result;" types)="" types[i].="" {="" {0}.",="" }="">   
 2/// get Method informations from MethodInfo[] Array:   
 3///    
 4/// <param name="myArrayMethodInfo"/>   
 5/// <returns></returns>   
 6public string getMethodInfo(MethodInfo[] myArrayMethodInfo  )   
 7{   
 8string methodStr="";   
 9///   
10///getinformation for all methods.   
11for(int i=0;i<myarraymethodinfo.length;i++) +i+"="" :"+="" <summary="" methodinfo="" methodstr+="Method " methodstr;="" mymethodinfo="(MethodInfo)myArrayMethodInfo[i];" mymethodinfo.name+environment.newline;="" return="" {="" }="">   
12/// get properties information from PropertyInfo[] Array:   
13///    
14/// <param name="myPropertyInfo"/>   
15/// <returns></returns>   
16public string getPropertyInfo(PropertyInfo[] myPropertyInfo)    
17{   
18string propStr="";   
19// Display information for all properties.   
20for(int i=0;i&lt;myPropertyInfo.Length;i++)   
21{   
22PropertyInfo myPropInfo = (PropertyInfo)myPropertyInfo[i];   
23propStr+="property "+i+":"+ myPropInfo.Name+" type:"+ myPropInfo.PropertyType+Environment.NewLine;   
24}   
25return propStr;   
26}</myarraymethodinfo.length;i++)></types.getlength(0);++i)>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus