如何禁止一个.NET的DLL类库引用类库、NET、DLL

2023-09-04 22:32:48 作者:越长大越有自己的个性

如何禁止DLL类库中的其他解决方案中引用?

How can I forbid dll class library to be referenced in other solutions?

推荐答案

您可以考虑增加一个 StrongNameIdentityPermission 您的类库匹配的程序的强名称你的不的希望能够使用它。

You could look into adding a StrongNameIdentityPermission to your class library that matches the strong name of the program you do want to be able to use it with.

另外,你可以探索使用 InternalsVisibleToAttribute ,尽管它可能需要在您的图书馆code一些设计变更。这应该既不组件签名,或两者都具有强名称签名,只要工作。在属性指定的参数应与公共密钥和您希望能够访问其内部成员的程序集的名称。

Alternatively, you could explore using InternalsVisibleToAttribute, although it may require some design changes in your library code. This should work as long as neither assembly is signed, or both are signed with a strong name. The argument specified on the attribute should match the public key and the name of the assembly that you want to be able to access its internal members.

不过说真的,这只会阻止别人谁不拼命用你的库。他们将不能够添加引用,但它并没有prevent有人通过反射绕过它或拆卸您的code。总有办法周围几乎你实现任何安全措施。

But really, this will only stop someone who isn't trying very hard to use your library. They won't be able to add a reference, but it doesn't prevent someone from bypassing it through Reflection or disassembling your code. There are always ways around almost any security measure that you implement.