What is reflection?

Source: Internet
Author: User

① What is reflection?
Reflection provides encapsulated assembly, module, and type objects.
You can use reflection to dynamically create instances of the type (see figure 4), bind the type to an existing object (this does not exist), or obtain the type from an existing object (see Figure ② ). Then, you can call methods of the type or access its fields and attributes.
The simplest reflection:
Using system;
Using system. reflection;
Namespace testreflection
{
Class Program
{
Static void main (string [] ARGs)
{
// Create two objects. [Does object and objetct seem to be the same ?? Even the prompts are the same !]
Object A = new ax ();
Object B = new axzhz ();
// Obtain the object type
New testobjecttype (). testobjecttypenow (A, B );
}
}

Class ax
{
}

Class axzhz
{
}

Class testobjecttype
{
// The default modifier of the constructor is private.
Internal void testobjecttypenow (Object A, object B)
{
Type TPA = A. GetType ();
Type TPB = B. GetType ();
Console. writeline (tPA. fullname );
Console. writeline (TPB. fullname );
Console. Readline ();
}
}
}
Output result:
Testreflection. Ax
Testreflection. axzhz
[Analysis] through the object instance (a, B), you can use the GetType () method to obtain the class to which the object belongs. Instead of the converted class, you can construct the class of this type.
[Application] to a variable/object instance, test which class it belongs to, along with the Assembly to which the class belongs
[Appendix] Another method for obtaining types is to use the type. GetType and assembly. GetType methods, for example:
Type T = type. GetType ("testreflection. Ax ");
It should be noted that we have mentioned the relationship between the namespace and accessories. To find a class, you must specify its accessories.
Type class: indicates the type declaration: class type, interface type, array type, value type, enumeration type, type parameter, generic type definition, and open or closed generic type. Dizzy, no research on generics.

② What is the use of the type instance we obtain?
I. Obtain the class name. For example, if the fullname attribute in the preceding example is returned, testreflection. Ax is returned.
This is also disgusting. The returned result is also returned using a. tostring.
Ii. Create an object of this class. You first obtain the class name ax through I.
Ax AX = (ax) activator. createinstance (TPA );
We all know it's an ax type. Why don't we get a new one ??? Chicken ribs.
The above [appendix] I really don't know what it is for. I know testreflection. Ax, just new one.
Iii. obtain information about the class to which the object belongs.
The related attributes of this class are obtained through TPA.
In fact, you can also obtain the relevant information of the class through the relevant attributes of A. It is easy to worry about, and I really don't know what the type class is.

③ A glimpse of this story reveals the secrets leaked by an object instance (this is cool)
Through an object instance, we can obtain the assembly of the class containing this object instance, and then obtain the information of the entire assembly.


Using system;
Using system. reflection;
Namespace testreflection
{
Class Program
{
Public static void main (string [] ARGs)
{
Object A = new ax ();
// Obtain the basic information of all classes of the Assembly to which the object belongs
New testobjecttype (). testobjecttypenow ();
}
}

Class ax
{
Internal int kkkkkkkk = 0;
Public int ooooooooo;
Private int property;

Public int Property
{
Get {return property ;}
Set {property = value ;}
}
Public void ()
{
Console. writeline ("ax's Function !~ ");
}
}

Class axzhz
{
}

Class testobjecttype
{
// The default modifier of the constructor is private.
Internal void testobjecttypenow (Object)
{
Type TPA = A. GetType ();
Assembly = tpa. assembly;
Type [] types = assembly. gettypes ();
Foreach (type in types)
{
Console. writeline ("[class name]" + type. fullname );
// Obtain the structure information of the type
Constructorinfo [] myconstructors = type. getconstructors ();
Show (myconstructors );
// Obtain the field information of the type.
Fieldinfo [] myfields = type. getfields ();
Show (myfields );
// Obtain method information
Methodinfo [] mymethodinfo = type. getmethods ();
Show (mymethodinfo );
// Obtain the Property Information
Propertyinfo [] myproperties = type. getproperties ();
Show (myproperties );
// Get event information. This project has no event, so it is commented out,
// In this way, you can obtain more type-related information.
// Eventinfo [] myevents = type. getevents ();
// Show (myevents );
}
Console. Readline ();
}
// Display the basic information of the array
Public void show (object [] OS)
{
Foreach (Object VaR in OS)
{
Console. writeline (var. tostring ());
}
Console. writeline ("----------------------------------");
}
}
}

[Note] Only public information can be obtained through testing.

④ Dynamically create an object instance [classic]
It is the basis for implementing the abstract factory and the core technology for implementing the abstract factory. Through it, You can dynamically create an object you want. The following example shows how to dynamically create an instance of chinesename or englishname.


1 using system;
Using system. reflection;
Namespace testreflection
{
Class axzhz_sreflectionexample
{
Public static void main ()
{
INAME name = abstractfactory. getname ();
Name. showname ();
}
}

Public class abstractfactory
{
Public static INAME getname ()
{
// The value of S will be dynamically obtained from web. config later
// Assign s to testreflection. ishishname. The English name is displayed.
String S = "testreflection. chinesename ";
INAME name = (INAME) Assembly. Load ("testreflection"). createinstance (s );
Return name;
}
}

// Declare an interface that displays the "name" Function
Public interface INAME
{
Void showname ();
}

// Implement the interface to display the Chinese name
Public class chinesename: INAME
{
Public void showname ()
{
Console. writeline ("My name is ax! ");
Console. Readline ();
}
}

// Implement the interface to display the English name
Public class englishname: INAME
{
Void INAME. showname ()
{
Console. writeline ("My name is axzhz! ");
Console. Readline ();
}
}
}
⑤ Obtain all the assemblies of the entire solution (this is a bit useful)
If you are not sure which assemblies are used in your solution, you can use the following method. If you want to get information in assembly, see ③

Using system;
Using system. reflection;

Namespace testreflection
{
Class showallassembly
{
Public static void main ()
{
// Obtain all the assemblies of the Solution
Assembly [] AX = appdomain. currentdomain. getassemblies ();
// Display the name of each Assembly through Traversal
Foreach (Object VaR in Ax)
{
Console. writeline ("assembly name:" + var. tostring ());
}
// Use a known Assembly name to create an assembly
// Display the location of the initial specified Assembly through the codebase attribute
Console. writeline ("Location of the initially specified Assembly testreflection:" + assembly. Load ("testreflection"). codebase );
Console. Readline ();
}
}
}

 

(Source: http://blog.csdn.net/shuilv2000/archive/2009/10/26/4728991.aspx)

What is reflection?

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.