Activator.CreateInstance(CollectionType) as EntityCollection ??

Posts   
 
    
ianvink
User
Posts: 394
Joined: 15-Dec-2006
# Posted on: 12-Jul-2007 13:17:34   

Hi,

I have the following code snippet. I need to create a collection so that I can call it's AddNew()

Called with:

Stuff(typeof(MyLLBLCollectionObject));

private void Stuff(Type CollectionType)
{
 ???     EntityCollectionBase<??> Collection = Activator.CreateInstance(CollectionType) as   EntityCollectionBase;
 EntityBase newlyAddedEntity = Collection.AddNew();
}

On the ??? line, how should I change it?

ianvink
User
Posts: 394
Joined: 15-Dec-2006
# Posted on: 12-Jul-2007 14:41:09   

I think this is the solution:

Change the Method signature to use generics for the entity and collection?

public static void Stuff<E,C>()
            where E: EntityBase, new() 
            where C : EntityCollectionBase<E>, new()
{
                EntityCollectionBase<E> Collection = new C(); 
                EntityBase Entity = new E();
}  

Is there a better way?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 12-Jul-2007 15:45:27   

That's the correct way of doing it. I don't think there is a better way.