What is assembly (ProgramSet )? 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 as followsCode: 1 Using System; 2 Using System. Collections. Generic; 3 Using System. text; 4 Using System. reflection; 5 6 Namespace Namespaceref 7 { 8 Class Program 9 { 10 Static Void Main ( String [] ARGs) 11 { 12 Country Cy; 13 String assemblyname = @" Namespaceref " ; 14 String Strongclassname = @" Namespaceref. China " ; 15 // Note: The class name must be a strong class name. 16 // Assemblyname can be found in assemblyinfo. CS of the project. 17 Cy = (Country) Assembly. Load (assemblyname). createinstance (strongclassname ); 18 Console. writeline (Cy. Name ); 19 Console. readkey (); 20 } 21 } 22 23 Class Country 24 { 25 Public String Name; 26 } 27 28 Class Chinese: Country 29 { 30 Public Chinese () 31 { 32 Name = " Hi! " ; 33 } 34 } 35 36 Class America: Country 37 { 38 Public America () 39 { 40 Name = " Hello " ; 41 } 42 } 43 } 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: 1 If (Strongclassname = " China " ) 2 Cy = New China (); 3 Else If (Strongclassname = " America " ) 4 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 ); Conclusion The Assembly class has many methods and attributes. Like type, the Assembly class has many functions for conversion between names and methods and attributes. By understanding these two classes in depth, you can understand how the general language layer works. |