在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
);
或者,知道 temp2
是 out
(注意 ref
和 out
之间的区别是经过检查的仅由 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/