4

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

假设我想在 C# 中使用一些调用,如下所示:

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

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

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

我的问题:我得到了下一个 ArgumentNullException:

值不能为空。\r\n参数名称: managedString。

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

4

3 回答 3

10

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

static String^ GetValue(String^ section, String^ key, String^ defaultValue)
{
    if (defaultValue == nullptr)
        // special case handling
    ....
}
于 2014-01-02T12:30:49.380 回答
1

Windows 运行时也有类似的限制。请参阅对类似问题详细回答。看起来你可能被困在离它很近的东西上。

于 2014-01-02T12:41:31.287 回答
0

也许您需要传递一个空字符串而不是 null。例如

string date = MyWrapper.GetValue("SystemSettings", "BuildDate", "");
于 2014-01-02T12:14:55.170 回答