編譯器錯誤 CS0842
更新:2007 年 11 月
錯誤訊息
在標記著 StructLayout(LayoutKind.Explicit) 的型別內部不能使用自動實作的屬性。
自動實作的屬性會有編譯器 (Compiler) 提供的支援欄位,而且原始程式碼無法存取該欄位。因此,它們與 LayoutKind.Explicit 不相容。
若要更正這個錯誤
- 將屬性製作為規則屬性,您可以在這個規則屬性內提供存取子 (Accessor) 主體。
範例
下列範例會產生 CS0842:
// cs0842.cs
using System;
using System.Runtime.InteropServices;
namespace TestNamespace
{
[StructLayout(LayoutKind.Explicit)]
struct Str
{
public int Num // CS0842
{
get;
set;
}
static int Main()
{
return 1;
}
}
}