我怎样才能使“内部” .NET 程序集的类和成员对 IronRuby 代码可见吗?
我的场景由以下几点组成。
- 我正在用 C# 开发一个打包的软件产品,
- 因为它是一个打包的产品,所以需要严格控制程序集的公共接口...
- 所有程序集均具有强名称
- 任何不一定是“公共”的类都是“内部”
- 我想为这些“内部”类编写单元测试,因为它们是代码的主体
最后....我想尝试用 Ruby 编写单元测试。
由于单元测试位于包含被测代码的程序集外部,因此每个被测程序集都需要有一个“InternalsVisibleTo”属性来指定单元测试程序集的名称。当然,这意味着 Ruby 单元测试必须编译为 .NET 程序集,以便可以通过这种方式进行访问。
这可以做到吗?如果是这样,怎么办?我在网上能找到的所有关于“编译 IronRuby”的内容都是关于从源代码构建实际的 IronRuby 运行时。
My scenario consists of the following points.
- I have a packaged software product I am developing in C#
- Since it is a packaged product, the public interfaces of the assemblies need to be tightly controlled...
- All assemblies are strong-named
- Any classes that don't absolutely have to be "public" are "internal"
- I want to write unit tests for those "internal" classes, since they are the bulk of the code
And finally.... I want to try writing the unit tests in Ruby.
Since the unit tests would be external to the assembly containing the code under test, the assemblies under test would each need to have an "InternalsVisibleTo" attribute specifying the name of the unit test assembly. Which of course would mean that the Ruby unit tests would have to compile down to a .NET assembly so they can be given access in this way.
Can this be done? If so, how? All I can find on the web about "compiling IronRuby" is about building the actual IronRuby runtime from source.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决方案非常简单(您甚至不需要将 IronRuby 代码编译为程序集)。
IronRuby解释器(ir.exe)支持命令行开关-X:PrivateBinding(这是区分大小写的,注意)。当解释器运行在私有绑定模式下时,您可以调用.NET类的内部成员甚至私有成员。
因此,要测试 .NET 类内部成员,请按如下方式运行测试代码(假设测试代码位于名为“test_my_code.rb”的文件中):
The solution is very simple (and you don't even need to compile your IronRuby code to an assembly).
IronRuby interpreter (ir.exe) supports a command line switch -X:PrivateBinding (this is case sensitive, pay attention). When the interpreter is running in the private binding mode, you can call internal and even private members of .NET classes.
Therefore, to test your .NET class internal members, run your test code as follows (assuming the test code is in a file named "test_my_code.rb"):