为什么对象实例中的枚举具有静态上下文?
Why an enum in an object instance has a static context?
我有以下 class:
public class HandleResourceReferencesParams
{
public Factory Factory { get; set; }
public DataObject Resource { get; set; }
public HandleAction Action { get; set; }
public enum HandleAction
{
Activate,
Disable,
Terminate
}
}
在以下代码中使用:
var parameters = new HandleResourceReferencesParams();
parameters.Factory = context.Factory;
parameters.Resource = resource;
parameters.Action = parameters.HandleAction.Terminate; // Does not compile
HandleResourceReferences(parameters);
通过使用 parameters.HandleAction
,我得到一个编译错误:
Cannot access static enum 'HandleAction' in non-static context
显然没有声明枚举 'static'。为什么从对象实例(非静态)引用它时它具有静态上下文?
编辑:
我已经找到了 Tim 提到的解决方案(顺便感谢)。我只是想了解为什么会出现此错误。
使用
parameters.Action = HandleResourceReferencesParams.HandleAction.Terminate;
您将一个枚举分配给实例-属性,但枚举本身就像一个静态变量。
因此您不能通过声明它的外部 class 的实例调用枚举。如果您尝试通过它的实例使用静态字段,这类似于编译器错误 class:
public class FooClass
{
public static string Foo = "Foo";
public string FooProp { get; set; }
}
您无法通过实例访问静态字段 FooClass.Foo
:
var foo = new FooClass();
foo.FooProp = foo.Foo; // does not compile either, you have to use FooClass.Foo
一个enum
consists of a set of named constants, const
隐含地static
。
为什么编译器不让我使用实例?因为它试图防止您犯明显的粗心错误。您不需要实例,所以不要使用它。
该错误消息很不幸,但您不能 执行 并不不幸...您正在尝试访问类型的成员,而不是类型的成员该类型的 实例 的成员,但您正在这样做 "via" 一个实例。
基本上,这段代码编译失败的原因是一样的:
Thread t = new Thread(...);
t.Start();
t.Sleep(1000); // Nope, Sleep is a static method
所有 嵌套类型实际上是静态成员,因为您不能拥有特定于包含类型实例的类型。
来自 C# 规范,第 10.3.7 节(强调我的):
When a field, method, property, event, operator or constructor declaration includes a static
modifier, it declares a static member. In addition, a constant or type declaration implicitly declares a static member.
所以枚举是类型的静态成员,尽管没有 static
修饰符。
I found how to make it compile, I am asking for the reason why it is not compiling how I did it
HandleAction.Terminate 是一个枚举值。
它的值不与 HandleResourceReferencesParams 的实例链接,而是嵌套在对象 HandleResourceReferencesParams 中的类型。
它是枚举的定义,而不是像 属性 这样的成员或将被实例化的变量。
嵌套定义(枚举、结构或 class)的唯一特殊性是它与其父项 class 的特定权限。
我有以下 class:
public class HandleResourceReferencesParams
{
public Factory Factory { get; set; }
public DataObject Resource { get; set; }
public HandleAction Action { get; set; }
public enum HandleAction
{
Activate,
Disable,
Terminate
}
}
在以下代码中使用:
var parameters = new HandleResourceReferencesParams();
parameters.Factory = context.Factory;
parameters.Resource = resource;
parameters.Action = parameters.HandleAction.Terminate; // Does not compile
HandleResourceReferences(parameters);
通过使用 parameters.HandleAction
,我得到一个编译错误:
Cannot access static enum 'HandleAction' in non-static context
显然没有声明枚举 'static'。为什么从对象实例(非静态)引用它时它具有静态上下文?
编辑: 我已经找到了 Tim 提到的解决方案(顺便感谢)。我只是想了解为什么会出现此错误。
使用
parameters.Action = HandleResourceReferencesParams.HandleAction.Terminate;
您将一个枚举分配给实例-属性,但枚举本身就像一个静态变量。
因此您不能通过声明它的外部 class 的实例调用枚举。如果您尝试通过它的实例使用静态字段,这类似于编译器错误 class:
public class FooClass
{
public static string Foo = "Foo";
public string FooProp { get; set; }
}
您无法通过实例访问静态字段 FooClass.Foo
:
var foo = new FooClass();
foo.FooProp = foo.Foo; // does not compile either, you have to use FooClass.Foo
一个enum
consists of a set of named constants, const
隐含地static
。
为什么编译器不让我使用实例?因为它试图防止您犯明显的粗心错误。您不需要实例,所以不要使用它。
该错误消息很不幸,但您不能 执行 并不不幸...您正在尝试访问类型的成员,而不是类型的成员该类型的 实例 的成员,但您正在这样做 "via" 一个实例。
基本上,这段代码编译失败的原因是一样的:
Thread t = new Thread(...);
t.Start();
t.Sleep(1000); // Nope, Sleep is a static method
所有 嵌套类型实际上是静态成员,因为您不能拥有特定于包含类型实例的类型。
来自 C# 规范,第 10.3.7 节(强调我的):
When a field, method, property, event, operator or constructor declaration includes a
static
modifier, it declares a static member. In addition, a constant or type declaration implicitly declares a static member.
所以枚举是类型的静态成员,尽管没有 static
修饰符。
I found how to make it compile, I am asking for the reason why it is not compiling how I did it
HandleAction.Terminate 是一个枚举值。 它的值不与 HandleResourceReferencesParams 的实例链接,而是嵌套在对象 HandleResourceReferencesParams 中的类型。
它是枚举的定义,而不是像 属性 这样的成员或将被实例化的变量。
嵌套定义(枚举、结构或 class)的唯一特殊性是它与其父项 class 的特定权限。