Dynamically creating generic collections : Collection<T> using .Net Framework 2

  • Thread starter Thread starter Andy
  • Start date Start date
A

Andy

Can anyone tell me if it is possible to dynamically create generic
collections?

simply put I want to be able to something like this :

public object test(SomeBaseClass param1)
{
if (param1.prop1 == "blah")
{
//logic to determine the type
return Collection<a class derived from SomeBaseClass>
}
else
{
//logic to determine the type
return Collection<a different class derived from SomeBaseClass>
}
}

where the type of param1 is a superclass, but the passed parameter is
derived from the superclass.

I basically need to be able to make this generic so I can dynamically create
the strongly typed collection from a type determined at runtime.

Thanks
Andy
 
I basically need to be able to make this generic so I can dynamically create
the strongly typed collection from a type determined at runtime.

You can do this with Reflection (System.Type.BindGenericParameters).




Mattias
 
Back
Top