I have a mthod by many if and else. How can i convert it by 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);
}
According to the specification, only sbyte, byte, short, ushort, int, uint, long, ulong, char, string, or enum-types can be used in a
switchstatement, so basically you can not switch on atypeobject.Now, what you can do is switching on the
Nameof the type, that’s just astringand it’s ok to switch on it.