InternalsVisibleTo属性不工作属性、工作、InternalsVisibleTo

2023-09-02 10:24:25 作者:尽量洒脱

我试图使用 InternalsVisibleTo 组件的属性,使我的内部类的.NET类库看到我的单元测试项目。出于某种原因,我不断收到一个错误信息,说:

I am trying to use the InternalsVisibleTo assembly attribute to make my internal classes in a .NET class library visible to my unit test project. For some reason, I keep getting an error message that says:

MyClassName'不可访问由于其保护级别

'MyClassName' is inaccessible due to its protection level

这两个组件都签订了和我有属性声明中列出了正确的密钥。任何想法?

Both assemblies are signed and I have the correct key listed in the attribute declaration. Any ideas?

推荐答案

您绝对确保你在属性中指定正确的公钥? 请注意,您需要指定完整的公钥,不只是公钥标记。它看起来是这样的:

Are you absolutely sure you have the correct public key specified in the attribute? Note that you need to specify the full public key, not just the public key token. It looks something like:

[assembly: InternalsVisibleTo("MyFriendAssembly,
PublicKey=0024000004800000940000000602000000240000525341310004000001000100F73
F4DDC11F0CA6209BC63EFCBBAC3DACB04B612E04FA07F01D919FB5A1579D20283DC12901C8B66
A08FB8A9CB6A5E81989007B3AA43CD7442BED6D21F4D33FB590A46420FB75265C889D536A9519
674440C3C2FB06C5924360243CACD4B641BE574C31A434CE845323395842FAAF106B234C2C140
6E2F553073FF557D2DB6C5")]

这是320左右的十六进制数字。不知道为什么你需要指定完整的公用密钥 - 只用于其它组件参照它会更容易有人恶搞朋友组装的身份公钥标记可能

It's 320 or so hex digits. Not sure why you need to specify the full public key - possibly with just the public key token that is used in other assembly references it would be easier for someone to spoof the friend assembly's identity.