Apologies if this question has been asked before, I couldn’t find anything similar.
I have an enumerable choice that comes into my class:
public enum OrdinalValue
{
None = 0,
Qualification = 1,
Career = 2,
Faculty = 3
}
Based on this enumerable do I have 3 property calls that brings back data from an entity collection for each of these:
public List<Qualification> ByQualification { get; set; }
public List<Career> ByCareer { get; set; }
public List<Faculty> ByFaculty { get; set; }
I want to create a generic method that returns the correct property, based on the enum passed back from the calling method, something like:
public List<T> GetEntities<T>(OrdinalValue ord)
{
List<T> value = default(List<T>);
// based on enum, cast the correct List<T> and return value
// something like:
if (ord == OrdinalValue.Career)
return (List<T>)Convert.ChangeType(this.ByCareer, typeof(T));
return value;
}
Is it possible?
It sounds like you want something like:
However, this doesn’t feel like a good design to me. It’s hard to suggest anything better without knowing more about the bigger picture, but it doesn’t really feel like a truly generic method.