c# - C++/CLI 和 .NET 输出字符串参数

标签 c# c++-cli

在C#中,我有这个方法(.net framework 2.0)

 public String Authenticate( String configUrl, out String tokenId)

我想从托管的 C++ 代码中调用它 我有

__authenticator->Authenticate(  gcnew System::String(hostUrl),gcnew System::String(temp));

但 tokenId 返回为真。

我已经看到一些关于在 C# 中使用 ^ % 的答案,但这就是无法编译。

最佳答案

public String Authenticate(String configUrl, out String tokenId)

这个

__authenticator->Authenticate(
    gcnew System::String(hostUrl),
    gcnew System::String(temp)
);

在 C# 中等同于(考虑到 Authenticate 的签名)

__authenticator.Authenticate(
    new String(hostUrl),
    out new String(temp)
);

但是在 C# 中你不能做一个 out new Something,你只能 out 到变量,字段...所以在 C# 中你需要做:

String temp2 = new String(temp);

__authenticator.Authenticate(
    new String(hostUrl),
    out temp2
);

并且,考虑到参数在 out 中,您可以:

String temp2;

__authenticator.Authenticate(
    new String(hostUrl),
    out temp2
);

现在,在 C++/CLI 中你有

System::String^ temp2 = gcnew System::String(temp);

__authenticator->Authenticate(
    gcnew System::String(hostUrl),
    temp2
);

或者,知道 temp2out(注意 refout 之间的区别是经过检查的仅由 C# 编译器,而不是由 C++/CLI 编译器)

// agnostic of the out vs ref
System::String^ temp2 = nullptr;

// or knowing that temp2 will be used as out, so its value is irrelevant
// System::String^ temp2;

__authenticator->Authenticate(
    gcnew System::String(hostUrl),
    temp2
);

关于c# - C++/CLI 和 .NET 输出字符串参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42839812/

相关文章:

c# - 如何从 HashSet 列表中删除 "\t"?

C#:在数据库中存储文件大小

c# - 使用 Action<> 委托(delegate)时 int 递增的奇怪行为

c# - 在 native dll 中创建 C++ 类以在 C# 中使用

c++ - 未处理的异常 : System. AccessViolationException:试图读取或写入保护

c# - 用于使用 C#、C++/CLI 和非托管 C++ 的应用程序的内存分析工具

c# - 可配置的 Windows 服务 - 如何以及在何处存储配置

c++ - 如何设置我的 datetimepicker 日期?

.net - 具有类型推导的 C++/CLI 静态对象导致未处理的运行时异常

c# - 模型和 Controller 之间的关注点分离