如果我在具有多個默認值的基礎對象中定義了ENUM。當我從基礎對象繼承時,我想添加更多選項,特定於繼承對象的ENUM列表。擴展從基礎對象繼承的ENUM值
例如我的基地可能有一個名字方向與價值ENUM:
無
ALL
停止
開始
我創建一個新類的呼叫羅盤它繼承了基類和如何加入遵循ENUM方向。
北
南
東
西
我創建一個新類的呼叫導航它繼承了基類和如何添加以下到ENUM方向。
左
右
所以,在我的繼承類haow做我延長ENUM。 我正在使用VB.NET。
如果我在具有多個默認值的基礎對象中定義了ENUM。當我從基礎對象繼承時,我想添加更多選項,特定於繼承對象的ENUM列表。擴展從基礎對象繼承的ENUM值
例如我的基地可能有一個名字方向與價值ENUM:
無
ALL
停止
開始
我創建一個新類的呼叫羅盤它繼承了基類和如何加入遵循ENUM方向。
北
南
東
西
我創建一個新類的呼叫導航它繼承了基類和如何添加以下到ENUM方向。
左
右
所以,在我的繼承類haow做我延長ENUM。 我正在使用VB.NET。
你不能這樣做,因爲枚舉是值類型,所以是密封的。
雖然他試圖解決一個CodeProject上的文章:http://www.codeproject.com/KB/cs/EnumBuilder.aspx
熟悉的System.Reflection和System.Reflection.Emit命名空間,但它可能會更好,如果你使用的ICollection(KeyValuePair的(字符串,整數)),或者這種性質的東西。
這裏是EnumBuilder的vb.net例如,大多數的這種直接從http://msdn.microsoft.com/en-us/library/system.reflection.emit.enumbuilder.aspx
Sub Main()
Dim abcList As New List(Of String)
For Each Item As String In [Enum].GetNames(GetType(FirstEnum))
abcList.Add(Item)
Next
For Each Item As String In [Enum].GetNames(GetType(SecondEnum))
abcList.Add(Item)
Next
For Each Item As String In [Enum].GetNames(GetType(ThirdEnum))
abcList.Add(Item)
Next
Dim currentDomain As AppDomain = AppDomain.CurrentDomain
' Create a dynamic assembly in the current application domain,
' and allow it to be executed and saved to disk.
Dim aName As System.Reflection.AssemblyName = New System.Reflection.AssemblyName("TempAssembly")
Dim ab As System.Reflection.Emit.AssemblyBuilder = currentDomain.DefineDynamicAssembly(_
aName, System.Reflection.Emit.AssemblyBuilderAccess.RunAndSave)
' Define a dynamic module in "TempAssembly" assembly. For a single-
' module assembly, the module has the same name as the assembly.
Dim mb As System.Reflection.Emit.ModuleBuilder = _
ab.DefineDynamicModule(aName.Name, aName.Name & ".dll")
Dim eb As System.Reflection.Emit.EnumBuilder = _
mb.DefineEnum("MyNewEnum", System.Reflection.TypeAttributes.Public, GetType(Integer))
' Define members, set values (integer)
Dim i As Integer = 0
For Each item As String In abcList
eb.DefineLiteral(item, i)
i += 1
Next
Dim finished As Type = eb.CreateType() 'At this point, your System.RuntimeType exists with a BaseType of System.Enum
ab.Save(aName.Name & ".dll")
'finished.getEnumNames returns data similar to a NameValueCollection(of string,int32)
End Sub
Enum FirstEnum
One
Two
Three
End Enum
Enum SecondEnum
Four
Five
Six
End Enum
Enum ThirdEnum
Seven
Eight
Nine
End Enum
複製