简体   繁体   English

有没有办法将.NET程序集的特权访问权限授予另一个程序集?

[英]Is there a way to grant privileged access to a .NET assembly to only one other assembly?

I have a class library assembly and some test code. 我有一个类库程序集和一些测试代码。 Because the test code needs to peek at the internals of the the class, it needs to be part of the assembly and because I don't want to have it run all by it's self, it needs have something public so that a different assembly can invoke it. 因为测试代码需要查看类的内部,它需要成为程序集的一部分,因为我不想让它全部由它自己运行,它需要公开的东西,以便不同的程序集可以调用它。 This ends up tacking on some code that shouldn't be there to the main assembly. 这最终会解决一些不应该存在于主程序集中的代码。

What I'd like is to place all the test code in the other assembly and somehow grant it privileges to access internals of the class. 我想要的是将所有测试代码放在另一个程序集中,并以某种方式授予它访问该类内部的权限。 I don't need this to be part of the final product, just as part of the debug/private builds. 我不需要将它作为最终产品的一部分,就像调试/私有构建的一部分一样。

Is that possible? 那可能吗?

自由使用internal访问修饰符并将InternalsVisibleTo属性添加到程序集。

You want System.Runtime.CompilerServices.InternalsVisibleTo() 你想要System.Runtime.CompilerServices.InternalsVisibleTo()

You can also use reflection to access internal / private members. 您还可以使用反射来访问内部/私有成员。 This is how MSTest does it. 这就是MSTest的工作方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM