Using your suggestion of MakeGenericType, I came up with the following demo.
While this works, is it necessarily the best/correct way to do it?
Thanks,
Dave
==================
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
namespace ConsoleApplication1
{
class Program
{
Dictionary<string, object> mylist = new Dictionary<string, object>();
private List<object> sampleData = new List<object>();
private Type[] dataTypes =
{
typeof(int),
typeof(string)
};
static void Main( string[] args )
{
Program p = new Program();
p.CreateList();
p.FillList();
p.DiplayList();
Console.Out.WriteLine( "\n\nPress enter to continue..." );
Console.In.ReadLine();
}
public Program()
{
sampleData.Add( 1 );
sampleData.Add( "Hello" );
sampleData.Add( 2 );
sampleData.Add( "World" );
}
public void CreateList()
{
foreach ( Type type in dataTypes )
{
Type newList = typeof( List<> ).MakeGenericType( type );
BindingFlags internalFlags =
BindingFlags.CreateInstance |
BindingFlags.Public |
BindingFlags.Instance;
object instance = Activator.CreateInstance(
newList, internalFlags, null, null, null, null );
mylist.Add( type.Name, instance );
}
}
public void FillList()
{
Type listType;
IList list;
foreach ( object data in sampleData )
{
listType = data.GetType();
list = mylist[listType.Name] as IList;
if ( list != null )
{
list.Add( data );
}
}
}
public void DiplayList()
{
foreach ( string keys in mylist.Keys )
{
IList list = mylist[keys] as IList;
foreach ( object data in list )
{
Console.Out.WriteLine( data.ToString() );
}
}
}
}
}