InternalsVisibleTo 不适用于所有程序集。有些有效,有些无效
我有一个 Common DLL,它有一些内部使用的内部方法。除了我还有其他 3 个项目 WebServices、UnitTests 和 PAT
在 Common 项目的 AssemblyInfo.cs 中,我添加了这些行:
[assembly: InternalsVisibleTo("WebServices, PublicKey=00240...b59a0")]
[assembly: InternalsVisibleTo("UnitTests, PublicKey=00240...dcf98")]
[assembly: InternalsVisibleTo("PATs, PublicKey=00240...e65cf")]
这些内部方法与 UnitTests 和 PAT 但 WebService 完美配合。奇怪的是,当我在WebService项目中编写代码时,我可以在IntelliSense框中看到这些内部方法。当我编写代码时没有错误警报,但当我构建项目时,我收到此错误消息
“Common”不包含“内部方法名称”的定义
当时,我认为程序集名称在某种程度上是错误的。我试图确保它确实是错误的:
[assembly: InternalsVisibleTo("WebSer, PublicKey=00240...b59a0")]
然后在 WebService 项目中,它在我使用通用内部方法而不使用此消息进行编译的行处立即变成红色:
此处无法访问内部方法“内部方法名称”
所以我认为我的程序集名称是正确的,但我仍然不知道为什么我不能在我的 WebServices 项目中使用内部方法。
I have a Common DLL which have some internal methods for internal usage. Beside I also have 3 other projects WebServices, UnitTests and PATs
In AssemblyInfo.cs of Common project, I added these lines:
[assembly: InternalsVisibleTo("WebServices, PublicKey=00240...b59a0")]
[assembly: InternalsVisibleTo("UnitTests, PublicKey=00240...dcf98")]
[assembly: InternalsVisibleTo("PATs, PublicKey=00240...e65cf")]
These internal methods work perfect with UnitTests and PATs but WebService. It's weird that I can see these internal methods in the IntelliSense box when I write code in WebService project. There is no error alert when I write code but when I build the project, I got this error message
'Common' does not contain a definition for 'internal method name'
At that time, I thought the assembly name is wrong in some way. And I try to make it's really wrong for sure:
[assembly: InternalsVisibleTo("WebSer, PublicKey=00240...b59a0")]
Then in the WebService project, it turns red immediately at the line I use Common internal methods without compiling with this message:
Cannot access internal method 'internal method name' here
So I think my assembly name is correct but I still don't know why I cannot use internal methods in my WebServices project.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在此处给出的第一条错误消息表明您使用了错误的命名空间:
这意味着您要使用的方法不存在。它很可能位于其他名称空间中 - 特别是如果您在类中使用扩展方法。
您可能已经注意到,在将错误的名称放入 InternalsVisibleTo 表达式后,您遇到的第二个错误是不同的:
只有第二个错误与将内部公开给其他 dll 有关。
也有可能 Visual Studio 已经疯了,重新启动/重建就足够了。
The first error message you gave here suggests that you are using wrong namespace:
It means that the method you want to use it's not there. It is in some other namespace most probably - especialy if you're using extension methods to your classes.
As you've probably noticed, the second error you've got, after putting wrong name to InternalsVisibleTo expression, is different:
Only the second error is related to exposing internals to other dll.
It's also possible that Visual Studio has gone crazy and restarting/rebuilding would be sufficient.