I have used a method in a class (Extensions) which can take user input trough an expression like classInstance.GetIndex(c=>c.DT):
public static int GetIndex<T, Tres>(this T Class, Expression<Func<T, Tres>> propertySelector)
{
var body = (MemberExpression)propertySelector.Body;
var propertyInfo = (PropertyInfo)body.Member;
List<KeyValuePair<PropertyInfo, int>> indexList = GlobalClassList;
int i = indexList.FindIndex(p => p.Key == propertyInfo);
return i;
}
The code allows the user input to be searched on a previous populated list of List<KeyValuePair<PropertyInfo, int>>. This allows me to save time looking for the class every time and using reflection.
I was trying to have something simpler where the argument could just be the class instance element classInstance.GetIndex(classInstance.DT) without having to use the (MemberExpression)propertySelector.Body
Is this possible?
Aucun commentaire:
Enregistrer un commentaire