Document directory
- Create a program set and a friend assembly in Visual Studio
- Create a signature assembly and a friend assembly in Visual Studio
I. Assembly concepts and features
An assembly is a basic unit of. Net-based application deployment, version control, reuse, activation scope, and security permissions. An assembly can appear in the form of an executable (.exe) file or a dynamic link library (. dll) file. It is a block generated by the. NET Framework. They provide information required to understand the type implementation when running in public languages. You can regard an assembly as a set of types and resources that constitute a logical functional unit and are generated to work together.
An assembly can contain one or more modules. For example, when planning a large project, several developers can take charge of separate modules and create a single assembly by combining all these modules.
An assembly has the following features:
The Assembly is implemented as a. EXE or. dll file.
By placing the assembly in the Global Assembly Cache, you can share the Assembly among multiple applications. The Assembly must be strongly named before it is included in the Global Assembly cache. For more information, see Assembly with strong names.
The Assembly is loaded to the memory only when necessary. If the Assembly is not used, it is not loaded. This means that the Assembly may be an effective way to manage resources in large projects.
Reflection can be used to obtain assembly information programmatically. For more information, see reflection.
If the purpose of loading an assembly is only to check it, use methods such as reflectiononlyloadfrom.
Assembly List
Each assembly has an assembly list ". Similar to a directory, the Assembly list contains the following:
1. ID of the Assembly (its name and version ).
2. File tables, all other files in the program set, such as any other assembly on which the. exe or. dll file created by the package is dependent, or even a bitmap file or a ReadMe file.
3. The Assembly reference list is a list of all external dependencies. These external dependencies are some. DLL files or other files required by your application that have been created by others. Assembly references include both global and private objects. Global Objects reside in the Global Assembly Cache (areas available for other applications, some similar to the System32 directory ). Private objects must be in the same level (or lower level) Directory of the application installation directory.
Because the Assembly contains information about content, version control, and dependencies, Applications created with Visual Basic and C # can run normally without relying on the registry value. The Assembly reduces. DLL conflicts, making applications more reliable and easier to deploy. In many cases, you only need to copy files based on the. NET application to the target computer to install the application.
Ii. Assembly Classification
1. Private assembly
Private assembly is the simplest assembly type. Private assembly is generally attached to some software and can only be used in the software. A common scenario with private assembly is to provide applications in the form of executable files or multiple libraries. The Code contained in these libraries can only be used for this application.
The system can ensure that private assembly is not used by other software, because the application can only load the assembly in the folder where the main execution file is located or in its subfolders.
Users generally want to install commercial software in their own directories so that the software package does not overwrite, modify, or load the private assembly of another software package. Private Assembly can only be used for your own software packages, so that you have more control over the software you use. Therefore, no security measures are required, this is because there is no risk that other commercial software will overwrite the original private assembly with a new version of assembly (except when the software specifically performs malicious damage operations ). The name does not conflict. If a class in a private assembly happens to have the same name as a class in a private assembly of another person, no problem occurs because a given application can only use the name of a private assembly.
Because private assembly is completely self-contained, the installation process is very simple. You only need to put the corresponding file in the corresponding folder of the file system (registry entry is not required). This process is called "0-impact (xcopy) installation ".
2. Shared assembly
Shared assembly is a public library that can be used by other applications. Because other software can access Shared assembly, some protection measures must be taken to prevent the following risks:
● Name Conflict. the type of the shared Assembly execution of another company has the same name as the type in its own shared assembly. This is a serious problem because the client code can theoretically access these sets at the same time.
● The Assembly is overwritten by different versions of the same assembly-the new version is incompatible with some existing client code.
The solution to these problems is to place the shared assembly in a specific subdirectory tree of the file system, called the Global Assembly high-speed cache (GAC ). Unlike private assembly, you cannot simply copy shared assembly to the corresponding folder. Instead, you need to install it in the cache.. Net tool to check the assembly, and set a small folder hierarchy in the Assembly Cache to ensure the integrity of the Assembly.
To avoid name conflicts, the shared assembly should specify a name based on the private key encryption method (the private assembly only needs to specify the same name as its main file name ). This name is called strongname and is unique. It must be referenced by the application that you want to reference the shared assembly.
Issues related to overwrite assembly can be solved by specifying version information in the assembly list, or by simultaneous installation.
A strongly-named Assembly contains four unique flag Assembly features: File Name (without extension), version number, language and cultural information (if any), and public key.
This information is stored in the Assembly List (manifest. The list contains the Assembly metadata and is embedded in a file of the Assembly.
The following string identifies four different assembly files:
"Mytype, version = 1.0.1.0,
Culture = neutral, publickeytoken = bf5779af662fc055"
"Mytype, version = 1.0.1.0,
Culture = en-US, publickeytoken = bf5779af662fc055"
"Mytype, version = 1.0.2.0,
Culture = neturl, publickeytoken = bf5779af662fc055"
"Mytype, version = 1.0.2.0,
Culture = neutral, publickeytoken = dbe4120289f9fd8a
There are some special steps to create and use a shared assembly,
1. Use sn.exeto create an encrypted file. sn.exe-K test. SNK
2. Add the attribute in the source code of the public assembly, [Assembly: assemblykeyfileattribute ("test. SNK")], and place it behind all using. Alternatively, you can use projectproperties-> signing-> sign the Assembly to specify the encrypted file.
3. Use gacutil-I test. DLL to install the shared assembly. After installation, the test file is displayed in the C:/Windows/Assembly directory.
3. youyuan assembly
A friend assembly is an assembly that can access other Assembly types and members of the friend (Visual Basic) or internal (C #) type. If you specify an assembly as a peer assembly, you do not need to mark the type and members as public so that other assembly can access them. In the following cases, it is especially convenient to use a friend assembly:
· In a unit test, the test code runs in another assembly, but you need to access members in the Assembly marked as friend (Visual Basic) or internal (C #) that is being tested.
· You are developing a class library. Additional parts of the library are included in a separate assembly, but you need to access members in an existing Assembly marked as friend (Visual Basic) or internal (C.
How to: create an unsigned friend assembly
Create a program set and a friend assembly in Visual Studio
1. Open the Visual Studio command prompt.
2. Create a visual basic or C # file named friend_signed_a. which contains the following code. This Code uses the internalsvisibletoattribute feature to declare friend_signed_ B as a friend assembly.
// friend_unsigned_A.cs
// Compile with:
// csc /target:library friend_unsigned_A.cs
using System.Runtime.CompilerServices;
using System;
[assembly: InternalsVisibleTo("friend_unsigned_B")]
// Type is internal by default.
class Class1
{
public void Test()
{
Console.WriteLine("Class1.Test");
}
}
// Public type with internal member.
public class Class2
{
internal void Test()
{
Console.WriteLine("Class2.Test");
}
}
3. Use the following command to compile and sign friend_signed_a.
csc /target:library friend_unsigned_A.cs
4. Create a visual basic or C # file named friend_unsigned_ B, which contains the following code. Since friend_unsigned_a specifies friend_unsigned_ B as a friend assembly, the code in friend_unsigned_ B can access the friend (Visual Basic) or internal (C #) type and members in friend_unsigned_a.
// friend_unsigned_B.cs
// Compile with:
// csc /r:friend_unsigned_A.dll /out:friend_unsigned_B.exe friend_unsigned_B.cs
public class Program
{
static void Main()
{
// Access an internal type.
Class1 inst1 = new Class1();
inst1.Test();
Class2 inst2 = new Class2();
// Access an internal member of a public type.
inst2.Test();
System.Console.ReadLine();
}
}
5. Use the following command to compile friend_signed_ B.
csc /r:friend_unsigned_A.dll /out:friend_unsigned_B.exe friend_unsigned_B.cs
The name of the Assembly generated by the compiler must match the membership Assembly name passed to the internalsvisibletoattribute feature. You can use the/out compiler option to explicitly set the assembly.
In C #, you must use the/out compiler to specify the name of the output Assembly (.exe or. dll. In Visual Basic, this is optional.
6. Run the file friend_signed_ B .exe.
The program prints two strings: "class1.test" and "class2.test ".
Security
Internalsvisibletoattribute is similar to strongnameidentitypermission. The main difference is that strongnameidentitypermission requires security permissions to run a specific piece of code, while the internalsvisibletoattribute feature controls the visibility of the friend (Visual Basic) or internal (C #) Types and members.
How to: create a signed friend assembly
Create a signature assembly and a friend assembly in Visual Studio
1. Open the Visual Studio command prompt.
2. Use the strong name tool to generate the Keyfile and display its public key using the command sequence below. For more information, see sn.exe ).
1. generate a strong name key for this example and store it in the friendassemblies. SNK file:
Sn-kfriendassemblies. SNK
2. Extract the public key from the file friendassemblies. SNK and put it in the file friendassemblies. publickey:
Sn-pfriendassemblies. SNK friendassemblies. publickey
3. display the public key stored in the friendassemblies. publickey file:
Sn-tpfriendassemblies. publickey
3. Create a visual basic or C # file named friend_signed_a, which contains the following code. This Code uses the internalsvisibletoattribute feature to declare friend_signed_ B as a friend assembly.
The strong name tool generates a new Public Key each time it runs. Therefore, you must replace the public key in the following code with the generated public key, as shown in the following example.
// friend_signed_A.cs
// Compile with:
// csc /target:library /keyfile:FriendAssemblies.snk friend_signed_A.cs
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("friend_signed_B, PublicKey=0024000004800000940000000602000000240000525341310004000001000100e3aedce99b7e10823920206f8e46cd5558b4ec7345bd1a5b201ffe71660625dcb8f9a08687d881c8f65a0dcf042f81475d2e88f3e3e273c8311ee40f952db306c02fbfc5d8bc6ee1e924e6ec8fe8c01932e0648a0d3e5695134af3bb7fab370d3012d083fa6b83179dd3d031053f72fc1f7da8459140b0af5afc4d2804deccb6")]
class Class1
{
public void Test()
{
System.Console.WriteLine("Class1.Test");
System.Console.ReadLine();
}
}
4. Use the following command to compile and sign friend_signed_a.
csc /target:library /keyfile:FriendAssemblies.snk friend_signed_A.cs
5. Create a visual basic or C # file named friend_signed_ B with the following code. Since friend_signed_a specifies friend_signed_ B as a friend assembly, the code in friend_signed_ B can access the friend (VisualBasic) or internal (C #) type and members in friend_signed_a. The file contains the following code.
// friend_signed_B.cs
// Compile with:
// csc /keyfile:FriendAssemblies.snk /r:friend_signed_A.dll /out:friend_signed_B.exe friend_signed_B.cs
public class Program
{
static void Main()
{
Class1 inst = new Class1();
inst.Test();
}
}
6. Use the following command to compile and sign friend_signed_ B.
This language is not supported or there are no available code examples.
csc /keyfile:FriendAssemblies.snk /r:friend_signed_A.dll /out:friend_signed_B.exe friend_signed_B.cs
The name of the Assembly generated by the compiler must match the name of the metaassembly passed to the internalsvisibletoattribute feature. You can use the/out compiler option to explicitly set the assembly.
In C #, you must use the/out compiler to specify the name of the output Assembly (.exe or. dll. In Visual Basic, this is optional.
7. Run the file friend_signed_ B .exe.
The program prints the string "class1.test ".
4. How to add shared assembly to reference
1. How to create a strong-name assembly
You can use vs to generate a public key pair file. Project Properties à signing
2. How to view public key token
Run the Sn-T [Assembly] command.
3. Install the shared assembly
Only a set of Strong names can be installed in GAC. You can use commands
Gacutil/I [Assembly]
4. Uninstall the shared assembly
In the C:/Windows/Assembly directory, right-click the assembly to be uninstalled, and select Uninstall.
5. How to add reference to shared assembly in
First Run Start> RUN> "C:/Windows/assembly ".
Select the folder of the Assembly you are looking for, enter the folder, and then select the correct version of the folder.
After finding the required assembly, you can temporarily copy it to a location, add the copied assembly to the reference, and then delete the temporary assembly copy, because Vs will automatically search for the Assembly in GAC.
V. How to: determine whether the file is an assembly
How to manually determine whether a file is an assembly
1. Start ildasm.exe (msil disassembly program ).
Load the file you want to test.
If ildasm reports that this file is not an executable (PE) file that can be migrated, it is not an assembly. For more information, see How to: view the Assembly content.
2. How to Determine programmatically whether a file is an assembly?
Call the getassemblyname method and pass the complete file path and name of the file being tested to it.
If a badimageformatexception is thrown, the file is not an assembly.