错误 cs0570 嵌套结构 "not supported by the language"
error cs0570 nested struct "not supported by the language"
在 C++/CLI 中:
[StructLayout(LayoutKind:Sequential, CharSet=CharSet::Ansi)]
public ref struct NestedStruct
{
public:
int x;
};
[StructLayout(LayoutKind:Sequential, CharSet=CharSet::Ansi)]
public ref struct AStruct
{
public:
NestedStruct nestedStruct;
};
在 C# 中:
AStruct s = new AStruct();
s.nestedStruct.x = 7; // ERROR cs0570
C++/CLI 是否禁止嵌套结构?结构是否应该在单独的 C# 程序集中定义?
NestedStruct nestedStruct;
那是个问题,NestedStruct
不是值类型。 ref struct
遵循本机 C++ 用法,其中结构和 class 之间没有真正的区别,只是结构仅具有其所有成员 public 默认情况下。 ref
关键字是真正重要的关键字,您声明了引用类型而不是值类型。引用类型的变量应该用帽子声明,以使其可供其他 .NET 语言使用。修复:
NestedStruct^ nestedStruct;
如果您实际上打算声明一个值类型(如 C# 中的结构),那么您必须这样写:
public value class NestedStruct
{
public:
int x;
};
或者因为您创建了成员 public,您可以使用 value struct
并删除 public:
,因为这是结构的默认可访问性。
Fwiw,此 "feature" 旨在让本地 C++ 程序员熟悉 C++/CLI 语法。没有。
在 C++/CLI 中:
[StructLayout(LayoutKind:Sequential, CharSet=CharSet::Ansi)]
public ref struct NestedStruct
{
public:
int x;
};
[StructLayout(LayoutKind:Sequential, CharSet=CharSet::Ansi)]
public ref struct AStruct
{
public:
NestedStruct nestedStruct;
};
在 C# 中:
AStruct s = new AStruct();
s.nestedStruct.x = 7; // ERROR cs0570
C++/CLI 是否禁止嵌套结构?结构是否应该在单独的 C# 程序集中定义?
NestedStruct nestedStruct;
那是个问题,NestedStruct
不是值类型。 ref struct
遵循本机 C++ 用法,其中结构和 class 之间没有真正的区别,只是结构仅具有其所有成员 public 默认情况下。 ref
关键字是真正重要的关键字,您声明了引用类型而不是值类型。引用类型的变量应该用帽子声明,以使其可供其他 .NET 语言使用。修复:
NestedStruct^ nestedStruct;
如果您实际上打算声明一个值类型(如 C# 中的结构),那么您必须这样写:
public value class NestedStruct
{
public:
int x;
};
或者因为您创建了成员 public,您可以使用 value struct
并删除 public:
,因为这是结构的默认可访问性。
Fwiw,此 "feature" 旨在让本地 C++ 程序员熟悉 C++/CLI 语法。没有。