gpt4 book ai didi

.net - 如何在托管 C++/CLI 项目中修复 CA2123(覆盖链接需求应与基本相同)

转载 作者:行者123 更新时间:2023-12-04 03:58:33 25 4
gpt4 key购买 nike

我无法理解如何为 C++/CLI 项目修复 CA2123。这是一个演示该问题的示例项目:

1) 创建一个 C# (.NET 4) 类库

ManagedClass.cs

命名空间 CSharpLibrary
{

public interface IManagedClass
{
void WriteSomething();
}

public class ManagedClass : IManagedClass
{
public void WriteSomething()
{
}
}

}

2)创建一个 C++/CLI 控制台应用程序(VS 2010):

装配信息.cpp
#include "stdafx.h"

using namespace System;
using namespace System::Reflection;
using namespace System::Runtime::CompilerServices;
using namespace System::Runtime::InteropServices;
using namespace System::Security;

[assembly:AssemblyTitleAttribute("CPlusPlusCLIConsoleApp")];
[assembly:AssemblyDescriptionAttribute("")];

[assembly:AssemblyVersionAttribute("1.0.*")];

[assembly:ComVisible(false)];

[assembly:CLSCompliantAttribute(false)];

[assembly:SecurityCritical];

CPlusPlusCLIConsoleApp.h
#pragma once

using namespace CSharpLibrary;
using namespace System::Security;

typedef void* (__cdecl FACTORY_PROC)();

namespace CPlusPlusCLIConsoleApp
{
public ref class MainClass : public IManagedClass
{
public:
[SecurityCritical]
virtual void WriteSomething();
};

};

CPlusPlusCLIConsoleApp.cpp
#include "stdafx.h"
#include "CPlusPlusCLIConsoleApp.h"

using namespace System;

int main(){};

namespace CPlusPlusCLIConsoleApp
{
[SecurityCritical]
void MainClass::WriteSomething()
{
}
};

启用所有 Microsoft 安全规则后,我收到以下警告:

CA2123 Override link demands should be identical to base

Add the following security attribute to 'MainClass::WriteSomething(void)' in order to match a LinkDemand on base method 'IManagedClass::WriteSomething(void)': 'SecurityCriticalAttribute'.

CPlusPlusCLIConsoleApp cpluspluscliconsoleapp.cpp 13



我试图按照这个 StackOverflow answer建议但它没有解决错误。

我知道托管 dll 默认为 SecurityCritical(我不想在我的原始项目中更改它),因为我没有指定任何 SecurityAttribute。为什么 C++ CLI dll 不遵循相同的默认值?

我应该遵循哪些步骤来修复此错误? (基本上我怎样才能在 C++ CLI 中创建 WriteSomething 方法 SecurityCritical)

编辑 1:我在 MSDN 上问过同样的问题.

编辑 2:联系了 Microsoft,这是一种设计行为。 C++\CLI 团队只是没有时间为 C++\CLI 实现 Level2 Security。因此 C++\CLI 总是停留在 Level1 Security。可以安全地抑制相同的代码分析警告。

最佳答案

这里的根本问题是您的 C# 和 C++ 程序集使用了两种不同的透明度模型(参见 http://blogs.msdn.com/b/shawnfa/archive/2009/11/11/transparency-models-a-tale-of-two-levels.aspxhttp://blogs.msdn.com/b/shawnfa/archive/2009/11/12/differences-between-the-security-rule-sets.aspx 了解这两个级别的详细信息)。这是因为默认情况下 C# 程序集编译为级别 2,但由于某些明显未记录的原因,编译器会自动将 C++ 程序集强制降至级别 1。不幸的是,后一种行为似乎不可替代。更糟糕的是,它在 VS2012 中似乎没有改变,it doesn't look like the product team is considering changing it any time soon .

鉴于您无法将 C++ 程序集移至级别 2,如果您想将可执行文件保留在 C++ 中并且它必须包含接口(interface)实现,则有几个可能可行的选择:

  • 通过使用将 C# 程序集移动到级别 1
    安全规则属性。这大概只有在
    C++ 控制台应用程序是 C# 库的唯一使用者。
  • 将安全关键性的 2 级“升级”再现到一个完整的
    通过使用 PermissionSetAttribute 信任链接/继承需求。
    例如。:
    [SecurityCritical]
    [PermissionSet(SecurityAction::LinkDemand, Unrestricted = true)]
    [PermissionSet(SecurityAction::InheritanceDemand, Unrestricted = true)]
    virtual void WriteSomething();

  • Connect 上提交另一个错误报告可能也是值得的。 (投票给封闭的似乎不是很有用)或 UserVoice 上的功能请求请求更改编译器行为。 (鉴于 2 级应该是 .NET 4.0 及更高版本的默认设置,锁定到 1 级是非常奇怪的。)

    关于.net - 如何在托管 C++/CLI 项目中修复 CA2123(覆盖链接需求应与基本相同),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17571948/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com