InternalsVisibleTo不适用于托管C ++

时间:2022-09-01 11:27:45

InternalsVisibleTo is not working for my managed C++ projects, but it is for my C# projects. Any help would be appreciated. Here is a simplified layout.

InternalsVisibleTo不适用于我的托管C ++项目,但它适用于我的C#项目。任何帮助,将不胜感激。这是一个简化的布局。

Project A - C#, has an internal property I want to access from B/C.
Project B - Managed C++. References A.
Project C - C#, references A.

项目A - C#,具有我想从B / C访问的内部属性。项目B - 托管C ++。参考文献A.项目C - C#,参考文献A.

All projects are signed with the same key. Looking at the compiled assemblies with ILDASM or Reflector show that they are all signed correctly (when I comment out the internal property usage).

所有项目都使用相同的密钥签名。使用ILDASM或Reflector查看已编译的程序集,表明它们都已正确签名(当我注释掉内部属性用法时)。

In AssemblyInfo.cs in Project A, I have the following InternalsVisibleTo;

在Project A的AssemblyInfo.cs中,我有以下InternalsVisibleTo;

[assembly: InternalsVisibleTo( "B, " +
   "PublicKey=00240000048000009400000006020000002400005253413100040000010001007" +
   "50098646D1C04C2A041FAAF801521A769535DE9A04CD3B4DEDCCBF73D1A6456BF4FE5881451" +
   "0E84983C72D0460B8BA85C52A9CACDC4A0785A08E247C335884C2049ECFE6B2C5E20A18FE4B" +
   "9BFF009ADA232E980D220B3C9586C9C5EE29C29AEE8853DB7BB90CF5A4C704F5244E1A1085C" +
   "4306008535049A0EBB00FE47E78DCB" )]

[assembly: InternalsVisibleTo( "C, " +
   "PublicKey=00240000048000009400000006020000002400005253413100040000010001007" +
   "50098646D1C04C2A041FAAF801521A769535DE9A04CD3B4DEDCCBF73D1A6456BF4FE5881451" +
   "0E84983C72D0460B8BA85C52A9CACDC4A0785A08E247C335884C2049ECFE6B2C5E20A18FE4B" +
   "9BFF009ADA232E980D220B3C9586C9C5EE29C29AEE8853DB7BB90CF5A4C704F5244E1A1085C" +
   "4306008535049A0EBB00FE47E78DCB" )]

The keys are cut'n'pasted, so I know they are correct.

钥匙是'切',所以我知道它们是正确的。

When I try to compile, A & C compile fine, but project B fails with

当我尝试编译时,A&C编译正常,但项目B失败了

Error 1 error C3767: 'A::MyClass::MyProperty::get': candidate function(s) not accessible c:\Users\<snip>\CppClass.cpp 201 B

The MSDN docs say this works with C++. Is there a bug or something else I need to do?

MSDN文档说这适用于C ++。我需要做一个bug还是别的什么?

Is there another way that I can protect a property so that it can only be used by assemblies signed by me? I know I can protect all of my assembly, but can I do it on a granular level like this?

还有另一种方法可以保护财产,使其只能由我签署的集合使用吗?我知道我可以保护我的所有组件,但是我可以在这样的粒度级别上进行吗?

EDIT

Based on comments in MSDN, I changed the attribute to the following, but that still doesn't work.

根据MSDN中的注释,我将属性更改为以下内容,但仍然无效。

[assembly: InternalsVisibleTo( "B, " +
   "PublicKey=00240000048000009400000006020000002400005253413100040000010001007" +
   "50098646D1C04C2A041FAAF801521A769535DE9A04CD3B4DEDCCBF73D1A6456BF4FE5881451" +
   "0E84983C72D0460B8BA85C52A9CACDC4A0785A08E247C335884C2049ECFE6B2C5E20A18FE4B" +
   "9BFF009ADA232E980D220B3C9586C9C5EE29C29AEE8853DB7BB90CF5A4C704F5244E1A1085C" +
   "4306008535049A0EBB00FE47E78DCB" ),
InternalsVisibleTo( "C, " +
   "PublicKey=00240000048000009400000006020000002400005253413100040000010001007" +
   "50098646D1C04C2A041FAAF801521A769535DE9A04CD3B4DEDCCBF73D1A6456BF4FE5881451" +
   "0E84983C72D0460B8BA85C52A9CACDC4A0785A08E247C335884C2049ECFE6B2C5E20A18FE4B" +
   "9BFF009ADA232E980D220B3C9586C9C5EE29C29AEE8853DB7BB90CF5A4C704F5244E1A1085C" +
   "4306008535049A0EBB00FE47E78DCB" )]

1 个解决方案

#1


26  

I found the answer to this. C++ works differently than the other languages. In addition to the InternalsVisibleTo, you must reference assembly A with the as_friend keyword. Since as_friend is not an option in the Add References dialog, you cannot add a project reference, instead, you need to add a reference in each CPP file that you needs it.

我找到了答案。 C ++与其他语言的工作方式不同。除了InternalsVisibleTo之外,还必须使用as_friend关键字引用程序集A.由于as_friend不是“添加引用”对话框中的选项,因此无法添加项目引用,而是需要在每个CPP文件中添加所需的引用。

#using <A.dll> as_friend

You then also need to change your assembly search path to include the build directory of project A.

然后,您还需要更改程序集搜索路径以包含项目A的构建目录。

IMHO, this is broken and typical of managed C++ being a second class language. Without the ability to do project references, you end up referencing the assembly in the build debug or release directory. This breaks dependencies and the only way you can get it to reference the correct DLL when you change your configuration from debug to release is with ugly #IFDEF DEBUG and relative paths for the #using.

恕我直言,这是破碎的,典型的托管C ++是第二类语言。如果没有项目引用的能力,最终会在构建调试或发布目录中引用程序集。这打破了依赖关系,当你将配置从debug更改为release时,你可以让它引用正确的DLL的唯一方法是使用丑陋的#IFDEF DEBUG和#using的相对路径。

I was also disappointed that this wasn't mentioned in the InternalsVisibleToAttribute documentation. I needed to dig around in the C++ documentation to find the information.

我也很失望InternalsVisibleToAttribute文档中没有提到这一点。我需要深入研究C ++文档以查找信息。

Edit: The documentation for InternalsVisisbleTo has since been updated with a link to the Friend Assemblies (C++) document.

编辑:此后,InternalsVisisbleTo的文档已更新为指向朋友程序集(C ++)文档的链接。

#1


26  

I found the answer to this. C++ works differently than the other languages. In addition to the InternalsVisibleTo, you must reference assembly A with the as_friend keyword. Since as_friend is not an option in the Add References dialog, you cannot add a project reference, instead, you need to add a reference in each CPP file that you needs it.

我找到了答案。 C ++与其他语言的工作方式不同。除了InternalsVisibleTo之外,还必须使用as_friend关键字引用程序集A.由于as_friend不是“添加引用”对话框中的选项,因此无法添加项目引用,而是需要在每个CPP文件中添加所需的引用。

#using <A.dll> as_friend

You then also need to change your assembly search path to include the build directory of project A.

然后,您还需要更改程序集搜索路径以包含项目A的构建目录。

IMHO, this is broken and typical of managed C++ being a second class language. Without the ability to do project references, you end up referencing the assembly in the build debug or release directory. This breaks dependencies and the only way you can get it to reference the correct DLL when you change your configuration from debug to release is with ugly #IFDEF DEBUG and relative paths for the #using.

恕我直言,这是破碎的,典型的托管C ++是第二类语言。如果没有项目引用的能力,最终会在构建调试或发布目录中引用程序集。这打破了依赖关系,当你将配置从debug更改为release时,你可以让它引用正确的DLL的唯一方法是使用丑陋的#IFDEF DEBUG和#using的相对路径。

I was also disappointed that this wasn't mentioned in the InternalsVisibleToAttribute documentation. I needed to dig around in the C++ documentation to find the information.

我也很失望InternalsVisibleToAttribute文档中没有提到这一点。我需要深入研究C ++文档以查找信息。

Edit: The documentation for InternalsVisisbleTo has since been updated with a link to the Friend Assemblies (C++) document.

编辑:此后,InternalsVisisbleTo的文档已更新为指向朋友程序集(C ++)文档的链接。