Iterate All Entities in a project

Posts   
 
    
ftuttle
User
Posts: 40
Joined: 10-Dec-2006
# Posted on: 11-Oct-2007 20:21:00   

Is there a way to iterate all of the entity Classes in a project.

I want to do something like this:

        For Each oBj In Project.EntityClasses
               'Do Something
        Next

thanks

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 11-Oct-2007 20:37:09   

Hi ftuttle, You could do something like:

Dim values As int[] = Enum.GetValues(typeof(EntityType)) As (int[])
For Each ent As EntityType In values
    'Do something, for instance, print entity name
    Console.WriteLine(ent.ToString())
Next

David Elizondo | LLBLGen Support Team
jmeckley
User
Posts: 403
Joined: 05-Jul-2006
# Posted on: 11-Oct-2007 21:51:26   
IList<IEntity> entities = new List<IEntity>();
GeneralEntityFactory factory = new GeneralEntityFactory();

foreach (int index in Enum.GetValues(typeof(EntityType)))
{
     entities.Add(factory.Create((EntityType)index));
}

foreach(IEntity entity in entities)
{
     //do something
}

ftuttle
User
Posts: 40
Joined: 10-Dec-2006
# Posted on: 11-Oct-2007 22:07:24   

Thanks for the responses.

simple_smile

Max avatar
Max
User
Posts: 221
Joined: 14-Jul-2006
# Posted on: 12-Oct-2007 10:18:34   

I use this way

    Public Shared Sub CreateEntityTypeDalDBlist()
        Dim typeArray() As Type
        Dim entityTypeList As New List(Of Type)

        Dim dalAssemblyReference As System.Reflection.Assembly

        'one of the entity
        Dim anEntity As New EntityClasses.AddressEntity

        'get a reference to the DAL assembly
        dalAssemblyReference = anEntity.GetType.Assembly


        'get all class/type in the assembly
        typeArray = dalAssemblyReference.GetExportedTypes()

        'check all class in the asembly
        Dim c As Integer
        For c = 0 To typeArray.Length - 1

            'check if this class/type is an entity
            If typeArray(c).Namespace.EndsWith(".EntityClasses") AndAlso (typeArray(c).GetInterface(GetType(IEntity2).FullName, True) IsNot Nothing) Then
                'it's an entity, add to the entity type list
                entityTypeList.Add(typeArray(c))
            End If
        Next
    End Sub