【问题标题】:CLI/C++ named ValueTuple like in C# (.Net Framework 4.7)CLI/C++ 在 C# (.Net Framework 4.7) 中命名为 ValueTuple
【发布时间】:2021-04-23 14:43:21
【问题描述】:
有没有办法在 CLI/C++ 中实现如下:
namespace Test
{
static class TestClass
{
static (int a, int b) test = (1, 0);
static void v()
{
var a = test.a;
var b = test.b;
_ = (a, b);
}
}
}
那么有没有办法在 CLI/C++ 中创建一个名称不同于 Item1 和 Item2 的 ValueTuple,以便它可以在 C# 中使用。
我正在使用 .Net Framework 4.7。
【问题讨论】:
标签:
c#
.net
c++-cli
valuetuple
【解决方案1】:
值的名称不是ValueTuple<...> 的一部分。
名称由编译器维护,属性在需要与外部代码通信时添加。
查看sharplab.io。
这个:
namespace Test
{
static class TestClass
{
static (int a, int b) test = (1, 0);
static void v()
{
var a = test.a;
var b = test.b;
_ = (a, b);
}
}
}
会翻译成这样:
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security;
using System.Security.Permissions;
namespace Test
{
internal static class TestClass
{
[TupleElementNames(new string[] {
"a",
"b"
})]
private static ValueTuple<int, int> test = new ValueTuple<int, int>(1, 0);
private static void v()
{
int item2 = test.Item1;
int item = test.Item2;
}
}
}