C#以更简单的方式使用类属性作为参数

时间:2019-01-12 15:29:31

标签: c# reflection

我有一种方法可以正常工作,该方法使用类属性作为参数来获取列表的索引。

代码:

sudo ln -s /usr/lib/libEGL1.so /usr/lib/libGL.so

调用方法:

public static int GetIndex<T, TKey>(this T myTypeClass, Expression<Func<T, TKey>> propertySelector)
{
    var body = (MemberExpression)propertySelector.Body;
    var propertyInfo = (PropertyInfo)body.Member;

    // list where I need to find the index for the property defined in the argument
    List<KeyValuePair<PropertyInfo, int>> indexList = GlobalClassList;

    // simple LINQ line to retrieve the int that corresponds with the 'propertyInfo'
    int i = indexList.FindIndex(p => p.Key.Equals(propertyInfo));
    return i;
}

我想做的是仅使用类(MyClass myClass = new MyClass(); myClass.GetIndex(c=>c.property) )的属性来简化方法中的参数,而不必依赖于c.property({{ 1}})。 这可能吗?

0 个答案:

没有答案
相关问题