2010-06-14 3 views
0

기본 개체에 여러 개의 기본값이있는 ENUM을 정의한 경우 기본 객체를 상속 할 때 상속 객체에 고유 한 ENUM 목록을 더 추가하고 싶습니다.기본 개체에서 상속 한 ENUM valus 확장

예를 들어 내 기본 값으로 방향라는 ENUM 수 :
없음
ALL
정지
시작을

나는 기본 클래스와 무엇을 추가 할을 상속하는 새로운 클래스 호출 나침반을 만들 ENUM 방향에 따라.
북한
사우스

웨스트

나는하여 ENUM 방향에 다음을 추가하는 기본 클래스와 무엇을 상속하는 새로운 클래스 호출 탐색을 만듭니다.
왼쪽
오른쪽

그래서, 할 haow 내 상속 클래스에서 나는 ENUM을 확장합니다. VB.NET을 사용하고 있습니다.

답변

0

가하는 System.Reflection 및 System.Reflection.Emit 네임 스페이스에 익숙해 있지만, 문자열 ((KeyValuePair의 ICollection에 사용하는 경우는 아마 더 나은, 정수)), 또는 그러한 성격의 어떤 것. 여기

가 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 
에서 복사