gpt4 book ai didi

c# - C# 中的字符串和 C++/CLI 中的字符串^...如何传递 "null"?

转载 作者:太空狗 更新时间:2023-10-29 19:52:44 24 4
gpt4 key购买 nike

我有一些 C++ 库。我还为它获得了 C++/CLI 包装器,以便可以在 C# 代码中从该库调用方法。

假设我想像这样在 C# 中使用一些调用:

string date = MyWrapper.GetValue("SystemSettings", "BuildDate", null);

它将在 C++/CLI 上调用下一个函数:

static String^ GetValue(String^ section, String^ key, String^ defaultValue)

我的问题:我遇到下一个 ArgumentNullException:

Value cannot be null.\r\nParameter name: managedString.

所以... 问题:我应该如何正确传递 null?谢谢。

最佳答案

您传递 null 的代码没有问题。问题是您的包装器代码需要检测 null 并进行处理。该代码大概是在第三个参数永远不为空的假设下编写的。如果您希望允许 null,则必须显式处理该条件:

static String^ GetValue(String^ section, String^ key, String^ defaultValue)
{
if (defaultValue == nullptr)
// special case handling
....
}

关于c# - C# 中的字符串和 C++/CLI 中的字符串^...如何传递 "null"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20882503/

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