用反射把程序集中一些泛型类实例化,怎么确定实例的类型?
代码如下,问题就出在红字部分:
Code
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
namespace Test
{
class CT { }
class CT1:CT { }
class CT2 : CT { }
class CT3 : CT { }
class CT4 : CT { }
class CT5 : CT { }
class CT6 : CT { }
class Base<T> : List<T> where T : CT { }
class class1: Base<CT1> { }
class class2: Base<CT2> { }
class class3: Base<CT3> { }
class class4: Base<CT4> { }
class class5: Base<CT5> { }
class class6: Base<CT6> { }
class test<T> where T : CT
{
private static Dictionary<string, Base<T>> d;
private static object obj = new object();
public static Dictionary<string, Base<T>> Caches
{
get
{
if (d == null)
{
lock (obj)
{
if (d == null)
Load();
}
}
return d;
}
}
private static void Load()
{
Type[] tys = Assembly.GetExecutingAssembly().GetTypes();
foreach (Type t in tys)
if (t.BaseType.Name == "Base`1")
{
XX x = (XX)Assembly.GetExecutingAssembly().CreateInstance("Test." + t.Name);
d.Add(t.Name, x);
}
}
}
}
两个 XX 应该是什么,怎么写?我头疼了半天,试了好几种方法,还是没搞定,请教各位,致谢先~
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
namespace Test
{
class CT { }
class CT1:CT { }
class CT2 : CT { }
class CT3 : CT { }
class CT4 : CT { }
class CT5 : CT { }
class CT6 : CT { }
class Base<T> : List<T> where T : CT { }
class class1: Base<CT1> { }
class class2: Base<CT2> { }
class class3: Base<CT3> { }
class class4: Base<CT4> { }
class class5: Base<CT5> { }
class class6: Base<CT6> { }
class test<T> where T : CT
{
private static Dictionary<string, Base<T>> d;
private static object obj = new object();
public static Dictionary<string, Base<T>> Caches
{
get
{
if (d == null)
{
lock (obj)
{
if (d == null)
Load();
}
}
return d;
}
}
private static void Load()
{
Type[] tys = Assembly.GetExecutingAssembly().GetTypes();
foreach (Type t in tys)
if (t.BaseType.Name == "Base`1")
{
XX x = (XX)Assembly.GetExecutingAssembly().CreateInstance("Test." + t.Name);
d.Add(t.Name, x);
}
}
}
}