I have a class file named ParentPropertyAttribute with a Type Property as below:
public Type PropertyType { get; set; }
In one of my class, I need to do some work based on the type passed. Right now, I am using if else condition as below:
if (parentPropertyAttribute.PropertyType == typeof(string))
{
return (parentList as IList<string>).Select(item => new SelectItem() { Value = item, Text = item }).OrderBy(selectItem => selectItem.Text);
}
else if (parentPropertyAttribute.PropertyType == typeof(XYZ))
{
return (parentList as IList<XYZ>).Select(x=> new SelectItem() { Value = item, Text = item }).OrderBy(selectItem => selectItem.Text);
}
The issue with the above is in future if there is any other type the if else case gets on increasing. Is there an optimized way to dynamically assign the type (string/XYZ in this case) to achieve this.
Any input is appreciated.
Regards.