[英]Is it guaranteed for two (or more) versions of the .NET Framework to be installed side by side?
[英]Are the Integer values of framework enums guaranteed to stay the same across .Net versions?
例如, 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,
}
那么,將該類型的值序列化為0、1、2或3是否安全? 也就是說,這些值是否一定會在將來的.Net版本中存在並且具有相同的含義?
該示例包含顯式聲明的值,因此即使您對行進行了重新排序,它們也永遠不會改變。 另外,每個枚舉值的值都是在編譯時確定的,因此,如果不重新編譯程序集,就無法更改它。
ECMA-334(C#)在21.3枚舉成員中指出...
枚舉成員的關聯值是隱式或顯式分配的。 如果枚舉成員的聲明具有常量表達式初始化程序,則該常量表達式的值(隱式轉換為枚舉的基礎類型)是枚舉成員的關聯值。 如果枚舉成員的聲明沒有初始化程序,則其關聯值將隱式設置,如下所示:
- 如果枚舉成員是用枚舉類型聲明的第一個枚舉成員,則其關聯值為零。
- 否則,通過將文本前面的枚舉成員的關聯值加1來獲得枚舉成員的關聯值。 該增加的值必須在基礎類型可以表示的值的范圍內。
如果使用隱式值,則可能會無意中更改枚舉值,並且1)對值進行重新排序,2)在值之前引入一個新成員或3)更改值之前的成員的值。
回答:
您專門詢問枚舉值的自定義序列化,因為它是基礎整數值。 .NET中不能保證這些值將保持不變,但是更改已部署的枚舉是一項重大更改, 很可能永遠不會發生。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.