[英]Get PropertyInfo of a parameter passed as lambda expression | Compact Framework, C#
說我有以下資產類別:
class Asset
{
public int Id { get; set; }
public string Name { get; set; }
}
現在,我想編寫一個方法GetPropertyInfo(a=>a.Name);
這個方法給了我Asset.Name的PropertyInfo。 我應該可以像這樣調用此方法:
編輯示例方法調用
PropertyInfo propInfo = GetPropertyInfo(a=>a.Name);
我有一個List<PropertyInfo>
所以我想將給定的lambda表達式與列表中的表達式匹配。
if(Possible on Compact Framework 3.5 && using C#)
How?
else
Please Notify
謝謝。
這可以在.NETCF 3.5下完成。
private List<Asset> m_list;
private Asset[] GetPropertyInfo(string name) {
var items = m_list.Where(a => a.Name == name);
if (items != null) {
return items.ToArray();
} else {
return null;
}
}
但是,您將需要初始化m_list
並首先將其填充數據。
更新:
因此,您的列表的類型為PropertyInfo
並且您希望調用以獲取與特定Asset
對象匹配的類型。
如果正確,則可以簡單地將上面的代碼編輯如下:
private List<PropertyInfo> m_list;
private PropertyInfo GetPropertyInfo(Asset a) {
return m_list.FirstOrDefault(x => x.Name == a.Name);
}
我不確定您如何獲取List<PropertyInfo>
。 我可以使用以下代碼提取單個PropertyInfo
對象:
private PropertyInfo GetPropertyInfo() {
var t = Type.GetType("System.Reflection.MemberInfo");
return t.GetProperty("Name");
}
這個項目沒有什么用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.