更新:没有举一个很好的例子。希望现在好一点。C#泛型:更好的方式来匹配泛型的类型?
难道还有比这更好的办法:
(typeof(TRepository) == typeof(UserClass))
下面是在写作中的运用:
public static IBaseRepository<TClass> GetRepository<TClass>() where TClass : IDataEntity
{
IBaseRepository<TClass> repository = null;
if (typeof(TClass) == typeof(UserClass))
{
repository = (IBaseRepository<TClass>)new UserClassRepository();
}
if (typeof(TClass) == typeof(PostClass))
{
repository = (IBaseRepository<TClass>)new PostClassRepository();
}
return repository;
}
如果像这样运行了很多,我希望有更好的办法比的typeof运行一堆。
什么是你想实现什么? 'typeof(TRepository)== typeof(FiftyPostUser)'应该总是* always *为false。 – Graviton 2009-12-15 01:14:24