Reflection reflection and attribute

Source: Internet
Author: User

Type T = typeof (myclass );
Object [] myobj = T. getcustomattributes (typeof (codereviewattrites), false );
Foreach (attribute a in myobj)
{
If (A is codereviewattri)
{
String abc = (codereviewattribute) a). reviewer; // get the custom attributes of the class.
}
}
Propertyinfo [] memberinfo = T. getproperties ();
Foreach (propertyinfo VaR in memberinfo)
{
Object [] myabc = var. getcustomattributes (false );

Foreach (attribute a in myabc)
{

If (A is codereviewattri)
{
String abc = (codereviewattribute) a). reviewer; // get the custom attribute on the attribute
Console. writeline (t + "class attributes include" + var. Name + "reviewer" + ABC );
}

}



}

Fieldinfo [] myinfo = T. getfields ();
For (INT I = 0; I <myinfo. length; I ++)
{
VaR myatt = myinfo [I]. getcustomattributes (false );
Foreach (attribute a in myatt)
{
If (A is codereviewattri)
{
Console. writeline (codereviewattrieline) a). reviewer );
}
}
}

String Path = application. startuppath + "\ calculator. dll ";
// Assembly assdmbly = assembly. LoadFile (PATH );
Assembly assdmbly = assembly. Load ("classlibrary1 ");
Console. writeline ("Get all classes in classlibrary1.dll ");
Console. writeline ("***********************************");
Foreach (type VaR in assdmbly. gettypes ())
{
Console. writeline (var. Name + "is the class in the classlibrary1 namespace ");
}
Console. writeline ("***********************************");
Console. writeline ("getting the module set in classlibrary1.dll ");
Module [] models = assdmbly. getmodules ();
Foreach (module VaR in models)
{
Console. writeline (var. Name + "is a module in classlibrary1 ");
}
Console. writeline ("***********************************");
Console. writeline ("");

Type type = typeof (classlibrary1.class1); // obtain the type of the specific class.
Console. writeline ("the specific type is" + type. Name );
Console. writeline ("{0} is not public type {1}", type, type. ispublic );
Console. writeline ("{0} is not private type {1}", type, type. isnotpublic );
Console. writeline ("***********************************");
Console. writeline ("");

Propertyinfo [] memberinfo = type. getproperties (); // obtain the attributes in the class.
Foreach (propertyinfo VaR in memberinfo)
{

Console. writeline (Type + "class attributes include" + var. Name );
Console. writeline (Type + "class attributes include" + var. Attributes. tostring ());
Propertyattributes Sf = var. attributes;
}
Console. writeline ("***********************************");
Console. writeline ("");
Type [] T = type. getinterfaces (); // obtain the interface
Foreach (type VaR in T)
{
Console. writeline (var. Name + "is the interface in classlibrary1 ");
}
Console. writeline ("***********************************");
Console. writeline ("");
Console. writeline ("return type of method, type of parameter passing by method ");

Memberinfo [] metho = type. getmethods (bindingflags. instance | bindingflags. nonpublic );
Foreach (memberinfo VaR in metho)
{
Console. writeline ("private return type: method name = {0} method information = {1}", var. Name, VAR );
}
Memberinfo [] methopublic = type. getmethods (bindingflags. instance | bindingflags. Public );
Foreach (memberinfo VaR in methopublic)
{
Console. writeline ("Public return type: method name = {0} method information = {1}", var. Name, VAR );
}
Memberinfo [] methostatic = type. getmethods (bindingflags. Public | bindingflags. Static );
Foreach (memberinfo VaR in methostatic)
{
Console. writeline ("common static return types: method name = {0} method information = {1}", var. Name, VAR );
}
Memberinfo [] methoprivatestatic = type. getmethods (bindingflags. Static | bindingflags. nonpublic );
Foreach (memberinfo VaR in methoprivatestatic)
{
Console. writeline ("Private Static return type: method name = {0} method information = {1}", var. Name, VAR );
}
Console. writeline ("***********************************");
Console. writeline ("");
Console. writeline ("this is the form of constructor ");
Constructorinfo [] con = type. getconstructors (); // get the constructor form
Foreach (constructorinfo VaR in Con)
{
Console. writeline (VAR );
}
Console. writeline ("***********************************");

Object OBJ = activator. createinstance (type, null); // create an instance without Parameters
// Call public non-static methods with parameters and return parameters
Methodinfo men1 = type. getmethod ("add ");
Console. writeline ("calling the {0} method", men1 );
Object [] nums1 = {5, 4}; // Parameter
Console. writeline (men1.invoke (OBJ, nums1 ));
Console. writeline ("***********************************");

// Call a private non-static method
Methodinfo men2 = type. getmethod ("add", bindingflags. instance | bindingflags. nonpublic );
Console. writeline (men2.invoke (OBJ, null ));
Console. writeline ("***********************************");
// Call public static methods with Parameters
Methodinfo men3 = type. getmethod ("multiplication", bindingflags. Public | bindingflags. Static );
Object [] num2 = {5, 6 };
Console. writeline (men3.invoke (null, num2 ));
Console. writeline ("***********************************");
// Private Static
Methodinfo men4 = type. getmethod ("multiplication", bindingflags. nonpublic | bindingflags. Static );
Console. writeline (men4.invoke (null, null ));
Console. writeline ("***********************************");
// Dynamically create a delegate
Console. writeline ("dynamically create a delegate ");
Testdelegate DELE = (testdelegate) Delegate. createdelegate (typeof (testdelegate), OBJ, "substation ");
// Console. writeline (DELE (9, 3 ));
Dele (9, 3 );

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.