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 );