C# 有没有一种简单的方法来使用 InternalsVisibleToAttribute?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/672501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there an easy way to use InternalsVisibleToAttribute?
提问by Mark Cidade
I have a C# project and a test project with unit tests for the main project. I want to have testable internal
methods and I want to test them without a magical Accessor object that you can have with Visual Studio test projects. I want to use InternalsVisibleToAttribute
but every time I've done it I've had to go back and look up how to do it, which I remember involves creating key files for signing the assemblies and then using sn.exe
to get the public key and so on.
我有一个 C# 项目和一个带有主项目单元测试的测试项目。我想拥有可测试的internal
方法,我想在没有 Visual Studio 测试项目可以拥有的神奇 Accessor 对象的情况下测试它们。我想使用,InternalsVisibleToAttribute
但每次我完成它时,我都必须回去查看如何使用它,我记得这涉及创建用于对程序集进行签名的密钥文件,然后使用它sn.exe
来获取公钥等等。
Is there a utility that automates the process of creating a SNK file, setting projects to sign the assemblies, extracting the public key, and applying the InternalsVisibleToattribute?
Is there a way to use the attribute withoutsigned assemblies?
是否有一个实用程序可以自动执行创建 SNK 文件、设置项目以对程序集进行签名、提取公钥以及应用InternalsVisibleTo属性的过程?
有没有办法在没有签名程序集的情况下使用该属性?
采纳答案by Brian Rasmussen
You don't have to use signed assemblies to use InternalsVisibleTo
. If you don't use signed assemblies, you can just enter the full name of the assembly.
您不必使用签名的程序集来使用InternalsVisibleTo
. 如果不使用签名程序集,则只需输入程序集的全名。
So if you want to have access to MyAssembly
in you test assembly (MyAssembly.Test
) all you need in AssemblyInfo.cs
for MyAssembly
is the name of the test assembly like this:
因此,如果您想MyAssembly
在测试程序集 ( MyAssembly.Test
) 中访问,那么您需要AssemblyInfo.cs
的MyAssembly
只是测试程序集的名称,如下所示:
[assembly: InternalsVisibleTo("CompanyName.Project.MyAssembly.Test")]
回答by Frederik Gheysels
Brian is right, but, offcourse in some cases you do have a signed assembly, and you do want to get the public key of that assembly.
Brian 是对的,但是,在某些情况下,您确实有一个签名的程序集,并且您确实希望获得该程序集的公钥。
Getting this key is indeed quite a hassle.
Therefore, I've done this:
I've customized my external tools in VS.NET, so that I can get the public key of the assembly of the current project with just one click of the mouse.
This is how it's done:
拿到这把钥匙确实很麻烦。
因此,我是这样做的:
我在VS.NET中自定义了我的外部工具,这样我只需单击鼠标即可获得当前项目的程序集的公钥。
这是它的完成方式:
- In VS.NET, open the 'Tools' menu, and click on 'External Tools'
- A window that is called 'External tools' will open
- Click on Add
- For title, type in 'Get public key'
- In 'Command', type in
C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\sn.exe
- In 'Arguments', type:
-Tp $(TargetPath)
- Make sure you check the 'Use output window' option
- 在 VS.NET 中,打开“工具”菜单,然后单击“外部工具”
- 将打开一个名为“外部工具”的窗口
- 点击添加
- 对于标题,输入“获取公钥”
- 在“命令”中,输入
C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\sn.exe
- 在“参数”中,键入:
-Tp $(TargetPath)
- 确保选中“使用输出窗口”选项
Then, you build the project, and when you click on 'Get public key', you should see the public key for this project in the output window.
然后,您构建项目,当您单击“获取公钥”时,您应该会在输出窗口中看到该项目的公钥。
回答by Manish Kumar
There is another alternate to call internal methods and that is using "Reflection".
还有另一种调用内部方法的替代方法,那就是使用“反射”。
BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.NonPublic;
MethodInfo minfo = typeof(BaseClassName).GetMethod("MethodName", bindingFlags);
BaseClassName b = new BaseClassName();
minfo.Invoke(b, null);
Here I assumes that "BaseClassName" is name of the class from another classlibrary and "MethodName" is name of internal method.
这里我假设“BaseClassName”是来自另一个类库的类的名称,“MethodName”是内部方法的名称。