ormvb.net-2010llblgenpro

How to return an EntityBase2 from a string?


I have a ListBox that is populated with my entity names, i.e. A1AllocationHelp1Entity

On select I need to pass that string name to get an EntityBase2 type.

I can get it using reflection:

Public Function CreateEntity(ByVal entityName As String) As EntityBase2
    Dim myAssembly = Assembly.LoadFrom(DALFileName)
    Dim assemblyName = Split(dynamicAssembly.FullName, ",")(0)
    Dim myEntityName = assemblyName & ".EntityClasses." & entityName
    Dim handle = Activator.CreateInstance(assemblyName, myEntityName)
    Dim entity = CType(handle.Unwrap(), EntityBase2)
    Return entity
End Function

but if I have the types there that llblgen generates, I would like to be able to instantiate it somehow without resorting to reflection.

Is there any way I can do that?


Solution

  • All that refection code can be replaced with a single line

    Return GeneralEntityFactory.Create( _
        CType(System.Enum.Parse(GetType(EntityType), entityName), EntityType))
    

    This returns the same type and is the answer I was looking for.