更新:之前的例子不太好,请看下面的例子。
有没有比下面这种方法更好的呢:
(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)
应该永远为false。 - Graviton