更新:2007 年 11 月
错误消息
无法使用对象初始值设定项为类型“struct name”的只读字段“name”的成员赋值,因为它是值类型。
只能在构造函数中分配那些为值类型的只读字段。
更正此错误
将结构更改为类类型。
使用构造函数初始化结构。
示例
下面的代码将生成 CS1917:
// cs1917.cs
class CS1917
{
public struct TestStruct
{
public int i;
}
public class C
{
public readonly TestStruct str = new TestStruct();
public static int Main()
{
C c = new C { str = { i = 1 } }; // CS1917
return 0;
}
}
}