How to return a EntityBase2 from a string?

Posts   
 
    
kooshka
User
Posts: 39
Joined: 14-Mar-2013
# Posted on: 27-Mar-2013 15:34:53   

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

I need to pass that string name and get the EntityBase2 type.

I can get it using reflection:


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

but if I have the types there I would like to be able to instantiate it somehow.

How can I do that?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 27-Mar-2013 18:07:49   
FactoryClasses.GeneralEntityFactory.Create(
            (EntityType)Enum.Parse(typeof(EntityType), entityAsString));

Sorry about the C# code simple_smile

kooshka
User
Posts: 39
Joined: 14-Mar-2013
# Posted on: 28-Mar-2013 09:22:20   

C# is just fine. That's Exactly what I needed. Thanks.

FYI, with VB.NET would be:


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