MustInherit and Enumeration RRS feed

  • Question




    I'm trying to build something like this:


    Code Block
    Customer.Find(new CustomerArgument(Field.FirstName,Operation.Like,"Name"))


    I'd like CustomerArgument and it's siblings to inherit from a BusinessObjectArgument class. It contains the logic for handling the operations and the last arguments like strings and integers etc ...


    This should then define the list of the fields so that they are exposed just like the first argument (Field.FirstName). However when I started coding this I noticed you can't declare an enumeration as mustoverride? How could you solve this? Or achieve a similar result?


    I was thinking about implementing an interface in the child classes so that they have to expose an enumeration but offcourse Interfaces can't do that Tongue Tied


    Code Block

    Public MustInherit Class BusinessObjectArgument

    Private _field As Fields = Nothing

    MustOverride Enum Fields ' <- doesn't work


    End Class




    edit: I'm looking into Reflaction now. Is it possible to expand an existing Enumeration with the properties of a certain Class? I think I'm able to read all the properties but how would I add them to the enumeration ... hmz.


    edit2: I'm looking into System.Reflection.Emit.EnumBuilder now.


    Am I getting close?

    Code Block

    Public Class BusinessObjectArgument(Of T As BusinessObject)

    Public Fields As [Enum]

    Public Sub New()

    Dim currentDomain As AppDomain = AppDomain.CurrentDomain

    Dim aName As AssemblyName = New AssemblyName("FieldsAssembly")

    Dim ab As AssemblyBuilder = currentDomain.DefineDynamicAssembly(aName, AssemblyBuilderAccess.RunAndSave)

    Dim mb As ModuleBuilder = ab.DefineDynamicModule(aName.Name, aName.Name & ".dll")

    ' this doesn't work ...

    Dim eb As EnumBuilder = mb.DefineEnum("Fields", TypeAttributes.Public, GetType(Integer))

    ' getting the properties

    Dim props() As PropertyInfo = GetType(T).GetProperties(BindingFlags.Public)

    'placing each property in the new enumeration

    Dim i As Integer = 0

    For Each p As PropertyInfo In props

    If p.MemberType = MemberTypes.Property Then

    eb.DefineLiteral(p.Name, i)

    i += 1

    End If


    Dim finished As Type = eb.CreateType()

    Fields = ' whatever I need to do to overwrite Fields with what I just made

    End Sub

    End Class




    edit3: To make it clear ... the enumeration should be listing it's literals when I do New CustomerArgument(Fields. ....)

    Fields should be the generated Enumeration. I'm starting to think though that this is very much impossible at runtime.

    Tuesday, December 18, 2007 2:10 PM