C # Use reflection to display all types and attributes of the Assembly

Source: Internet
Author: User

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 <types. GetLength (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;
}

}

}

/// <Summary>
/// Get Method informations from MethodInfo [] Array:
/// </Summary>
/// <Param name = "myArrayMethodInfo"> </param>
/// <Returns> </returns>
Public string getMethodInfo (MethodInfo [] myArrayMethodInfo)
{
String methodStr = "";
///
/// Getinformation for all methods.
For (int I = 0; I <myArrayMethodInfo. Length; I ++)
{
MethodInfo myMethodInfo = (MethodInfo) myArrayMethodInfo [I];
MethodStr + = "Method" + I + ":" + myMethodInfo. Name + Environment. NewLine;
}
Return methodStr;
}

/// <Summary>
/// Get properties information from PropertyInfo [] Array:
/// </Summary>
/// <Param name = "myPropertyInfo"> </param>
/// <Returns> </returns>
Public string getPropertyInfo (PropertyInfo [] myPropertyInfo)
{
String propStr = "";
// Display information for all properties.
For (int I = 0; I <myPropertyInfo. Length; I ++)
{
PropertyInfo myPropInfo = (PropertyInfo) myPropertyInfo [I];
PropStr + = "property" + I + ":" + myPropInfo. Name + "type:" + myPropInfo. PropertyType + Environment. NewLine;
}
Return propStr;
}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.