[英]Are the Integer values of framework enums guaranteed to stay the same across .Net versions?
For example, System.Windows.VerticalAlignment
is defined like this: 例如,
System.Windows.VerticalAlignment
的定义如下:
// Summary:
// Describes how a child element is vertically positioned or stretched within
// a parent's layout slot.
public enum VerticalAlignment
{
// Summary:
// The element is aligned to the top of the parent's layout slot.
Top = 0,
//
// Summary:
// The element is aligned to the center of the parent's layout slot.
Center = 1,
//
// Summary:
// The element is aligned to the bottom of the parent's layout slot.
Bottom = 2,
//
// Summary:
// The element is stretched to fill the entire layout slot of the parent element.
Stretch = 3,
}
So, is it safe to serialize a value of this type as 0, 1, 2, or 3? 那么,将该类型的值序列化为0、1、2或3是否安全? That is, are those values guaranteed to exist in future .Net versions, and have the same meaning?
也就是说,这些值是否一定会在将来的.Net版本中存在并且具有相同的含义?
The example contains explicitly declared values, so they would never change even if you reordered the rows. 该示例包含显式声明的值,因此即使您对行进行了重新排序,它们也永远不会改变。 Also, the value of every enum value is determined at compile-time, so it can not change without a recompilation of your assembly.
另外,每个枚举值的值都是在编译时确定的,因此,如果不重新编译程序集,就无法更改它。
ECMA-334 (C#) states in 21.3 Enum members that... ECMA-334(C#)在21.3枚举成员中指出...
The associated value of an enum member is assigned either implicitly or explicitly.
枚举成员的关联值是隐式或显式分配的。 If the declaration of the enum member has a constant-expression initializer, the value of that constant expression, implicitly converted to the underlying type of the enum, is the associated value of the enum member.
如果枚举成员的声明具有常量表达式初始化程序,则该常量表达式的值(隐式转换为枚举的基础类型)是枚举成员的关联值。 If the declaration of the enum member has no initializer, its associated value is set implicitly, as follows:
如果枚举成员的声明没有初始化程序,则其关联值将隐式设置,如下所示:
- If the enum member is the first enum member declared in the enum type, its associated value is zero.
如果枚举成员是用枚举类型声明的第一个枚举成员,则其关联值为零。
- Otherwise, the associated value of the enum member is obtained by increasing the associated value of the textually preceding enum member by one.
否则,通过将文本前面的枚举成员的关联值加1来获得枚举成员的关联值。 This increased value must be within the range of values that can be represented by the underlying type.
该增加的值必须在基础类型可以表示的值的范围内。
You can inadvertently change an enum value if you use implicit values, and 1) reorder the values, 2) introduce a new member preceding your value, or 3) change the value of a member preceding your value. 如果使用隐式值,则可能会无意中更改枚举值,并且1)对值进行重新排序,2)在值之前引入一个新成员或3)更改值之前的成员的值。
Answer: 回答:
You specifically ask about custom serialization of an enum value as it's underlying integer value. 您专门询问枚举值的自定义序列化,因为它是基础整数值。 There is no guarantee within .NET that the values will stay unchanged, but changing deployed enums is a breaking change which will most probably never occur.
.NET中不能保证这些值将保持不变,但是更改已部署的枚举是一项重大更改, 很可能永远不会发生。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.