CSharp Keyword -- Assembly ()

Source: Internet
Author: User

What is Assembly )?
Assembly is a collection of information such as the program name, version number, self-description, file association, and file location. The Assembly class is supported 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 information required for programming, such as program classes and instances.
A simple demo:
1. Create a Console project named NamespaceRef
2. Write the following code:

Using System; using System. collections. generic; using System. text; using System. reflection; namespace NamespaceRef {class Program {static void Main (string [] args) {Country cy; string assemblyName = @ "NamespaceRef"; string strongClassName = @ "NamespaceRef. china "; // note: the class name must be a strong class name.
// AssemblyName can be found in AssemblyInfo. cs of the project.
             cy = (Country)Assembly.Load(assemblyName).CreateInstance(strongClassName);             Console.WriteLine(cy.name);             Console.ReadKey();         }     } class Country { public string name;     } class Chinese : Country { public Chinese() {             name = "Äã?ºÃ?";         }     } class America : Country { public America() {             name = "Hello";         }     }}

The existence of Assembly gives us a better choice in implementing the design pattern.
We sometimes encounter such a problem during development, and create the specified object according to the corresponding name. For example, if chinese is given, a chinese object must be created. In the past, we could only write code like this:

if (strongClassName == "China")     cy = new China();else if (strongClassName == "America")     cy = new America();

So if we have a long series of objects to create, it is very difficult to maintain such code, and it is not easy to read. Now we can get an instance by defining the Assembly name and strong name of the class in an external file, which is easy to understand and enhances the scalability without modifying the code.
Cy = (Country) Assembly. Load (assemblyName). CreateInstance (strongClassName );

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.