Reflection simple example [rotate]

Source: Internet
Author: User

What is assembly (assembly)?
Assembly is a collection of information that contains the name, version number, self-description, file association, and file location of the program. Supported by the Assembly class in the. NET Framework, the class is located under System.Reflection, and the physical location is located at: mscorlib.dll.

What can assembly do?
We can use assembly information to obtain the program's class, instance, and other programming needs.

A simple Demo Example:
1. Establish a console project named: Namespaceref
2. Write the following code:

2UsingSystem.Collections.Generic;
3UsingSystem.Text;
4UsingSystem.Reflection;
5
6NamespaceNamespaceref
7{
8ClassProgram
9{
10 Static voidMain (String[] args)
11{
12Country CY;
13String AssemblyName= @"Namespaceref";
14StringStrongclassname= @"Namespaceref.china";
15 //Note: The class name must be a strong class name here
16//AssemblyName can be found through the AssemblyInfo.cs of the project
17Cy=(country) Assembly.Load (AssemblyName). CreateInstance (Strongclassname);
18Console.WriteLine (Cy.name);
19Console.readkey ();
20}
21st}
22
23ClassCountry
24{
25Public StringName
26}
27
28ClassChinese:country
29{
30PublicChinese ()
31{
32Name= "How are you doing";
33}
34}
35
36ClassAmerica:country
37{
38Public America ()
39         {
40              name = "hello" 41         }
42     }
43


Because of the existence of assembly, we have a better choice in implementing the design pattern.
We sometimes encounter such a problem when we develop, to create the specified object according to the corresponding name. Such as: Give Chinese to create a Chinese object, previously we can only write code like this:

= new China ();
3else if (strongclassname = = america ""
4      cy = new< Span style= "color: #000000;" > America ();

So if we have a long list of objects to create, this code is difficult to maintain and not easy to read. Now we can get an example by defining the assembly name of the class and the strong name of the class in the external file, which is easy to understand and enhances extensibility without modifying the code.
cy = (country) assembly.load (AssemblyName). CreateInstance (Strongclassname);

Conclusion
The Assembly class has a number of methods and properties, which, like type, have many functions for conversions between names and methods and properties. With a deep understanding of these two classes, you can clearly see how the common language layer works.

Http://www.cnblogs.com/muou/archive/2009/07/08/1518971.html

Reflection simple example [rotate]

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.