Try this:
public void SomeMethod()
{
var vars = new object();
var genericMethod = GetType().GetMethod("load");
var types = new[] { typeof(Settings), typeof(CatPictures),
typeof(OtherMemes) };
foreach(var type in types)
{
genericMethod.MakeGenericMethod(type).Invoke(this, new[] { vars });
}
}
There are some optimisations that could make this better but it's a good
start.
Michael M. Minutillo
Indiscriminate Information Sponge
http://codermike.com
On Wed, Oct 12, 2011 at 1:42 PM, Preet Sangha <[email protected]> wrote:
> So you want to create the list generically?
>
> This SO question should help:
>
>
> http://stackoverflow.com/questions/1371745/build-c-generic-type-defintion-at-runtime
>
> so my suggestion is to create a List<X> instance for each type
> (Activator.CreateInstance) in your types array and store the lists in an
> IList[]. Finally iterate over this IList[] to call add on each one with the
> vars entity.
>
>
> On 12 October 2011 18:23, Les Hughes <[email protected]> wrote:
>
>>
>> Hey all,
>>
>> It's kind of difficult to explain what I am looking to do, and it might
>> seem a little wacky, but here goes a simplified version of the question....
>>
>> I've got some lists of data in a database which I have a GenericList
>> interface for (from the database), so no matter what type of data is in the
>> DB, it has a way of displaying it as a GenericList.
>>
>> Each bit of data can be loaded into a data cache by using:
>>
>> public void loadData<T>(vars) where T : GenericList
>>
>> To load lots of data at once I would do this
>>
>> void someMethod()
>> {
>> ClassType vars = new ClassType();
>>
>> loadData<Name.Space.Settings>(**vars); // load settings
>> loadData<Name.Space2.**CatPictures>(vars); // load cat pictures
>> loadData<Name.Space2.**OtherMemes>(vars); // load other memes
>> }
>>
>>
>> What I am wondering if I can do something like this: (obviously the code
>> below isn't valid, but it should illustrate what I am trying to do)
>>
>> void someMethod()
>> {
>>
>> Type[] typeArray = new type[]{ Name.Space.Settings,
>> Name.Space2.catPictures, Name.Space2.otherMemes };
>> ClassType vars = new ClassType();
>> for (int i = 0; i < typeArray.length; i++)
>> {
>> loadData<typeArray[i]>(vars); // load each one of those types
>> }
>> }
>>
>> ------------------------------**-------------------
>>
>> Any ideas? Thanks heaps :)
>> --
>> Les Hughes
>> [email protected]
>>
>>
>
>
> --
> regards,
> Preet, Overlooking the Ocean, Auckland
>