Remove dependency in DLL in. net

Source: Internet
Author: User
We are doing. net class library projects often encounter such problems. I want to generate a DLL for other programs, but in my project, I need to reference some other references, like ActiveX controls or other DLL, these controls or DLL do not have strong name and cannot be written to GAC (Global Assembly Cache). Therefore, when registering your DLL, you must add the referenced controls or DLL to complete registration. However, this is not what we need. We just want to provide a DLL for people to use. Why do we need to bring a bunch of controls or DLL? Others do not want. So, how can we solve this problem?

This is the case for the current project. I have read a lot of online materials, so I will share my solution. please correct me if there is anything bad.

As you all know. net. If you try to write a DLL to GAC, you must use strong name for your DLL project, other controls or DLL used in your project must have strong name. Otherwise, when you register your DLL, a message indicating a lack of dependency is displayed. This is annoying, but it should be advantageous for version control. In this case, we only need to add strong name to the referenced control or DLL and write it to GAC, so that when registering your DLL, you will go to the GAC to find the control or DLL it uses. So, how to do it? There are roughly two steps:

1. Add strong name to the control or DLL to be referenced

This is required. Otherwise, you cannot write them into GAC .. Net SDK provides a tool to generate strong names for the Assembly. In fact, it is not only generated, but also management and signature verification, but also based on different options. For more information, see msdn. The following describes how to create a key pair using the Sn and store it in Keyfile. SNK:

Sn-K Keyfile. SNK

In this way, a key pair is generated. If your project does not reference other control or DLL without strong names, you just want to write your own DLL to GAC, then assemblyinfo in your project. in CS, add assemblykeyfile, which is the generated Keyfile. SNK file.

[Assembly: assemblykeyfile (".. \ Keyfile. SNK")]

The path here refers to the relative path of the project output after compilation. Therefore, if it is written here (you can modify it), you should set the generated Keyfile. copy the SNK file to the project directory to ensure that the SNK file can be found during compilation. In this way, the compiled dll can be written to GAC:

Gacutil-I yourdll. dll

If your project references other control or DLL without strong names, you need to perform the next step.

2. Add a strong name to a control or DLL without a strong name

Two tools are used here. Net comes with tlbimp and aximp. Tlbimp converts a type definition in a com-type library to an equivalent definition of a CLR assembly, and aximp converts a type definition in a com-type library to a Windows form control. For details, see msdn.
Suppose you want to add a strong name to Tom. dll and msflxgrd. ocx:
Tlbimp Tom. dll Keyfile: Tom. dll. SNK out: Tom. dll

Aximp msflxgrd. ocx Keyfile: msflxgrd. ocx. SNK axmsflxgrd. ocx. SNK

Here, Tom. dll. SNK, msfllxgrd. ocx. SNK, and axmsflxgrd. ocx. SNK are generated based on the first step. Each DLL corresponds to an SNK. Tom. dll is the name of the converted DLL, which has no other meaning. In addition, msflxgrd. ocx generates two DLL types: msflexgridlib. dll and axmsflexgridlib. dll. I personally think that an axmsflexgridlib will be generated during form initialization. I wonder who has a better explanation?

In this way, the Tom. dll and msflxgrd. ocx controls are added with a strong name and converted to the corresponding DLL, and then written to the GAC:

Gacutil-I Tom. dll
Gacutil-I msflexgridlib. dll
Gacutil-I axmsflexgridlib. dll

The rest of the work is to re-reference them to your project and re-compile them so that the generated DLL does not need any dependency during registration because they are already in GAC, open c: \ winnt \ assembly.

If you want to delete them from GAC, then:

Gacutil-u Tom
Gacutil-u msflexgrid
Gacutil-u axmsflexgrid Tom, msflexgrid, and axmsflexgrid are the names in GAC. No path is required here.

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.