【发布时间】:2010-12-26 15:42:13
【问题描述】:
更新:没有给出一个很好的例子。希望现在好多了。
有没有比这更好的方法:
(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)应该总是总是为假。