我有一个由许多if 和else
组成的方法。我如何通过 Switch
转换它?
protected override IRepository<T> CreateRepository<T>()
{
if (typeof(T).Equals(typeof(Person)))
return new PersonRepositoryNh(this, SessionInstance) as IRepository<T>;
else if (typeof(T).Equals(typeof(Organization)))
return new OrganizationRepositoryNh(this, SessionInstance) as IRepository<T>;
else
return new RepositoryNh<T>(SessionInstance);
}
最佳答案
根据specification , 只有 sbyte, byte, short, ushort, int, uint, long, ulong, char, string, or enum-types 可以在 switch
语句中使用,所以基本上你不能打开 type
对象。
现在,您可以做的是打开类型的 Name
,它只是一个 string
,打开它就可以了。
关于c# - 如何在此示例方法中使用 (Switch) 而不是 (IF & Else),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8373303/