工作在.NET 2.0這裏,反射一般列表。我最初使用t.IsAssignableFrom(typeof(IEnumerable))
來檢測我正在遍歷的屬性是否支持IEnumerable
接口。 (因此我可以安全地將對象投射到它)
但是,當對象爲BindingList<T>
時,此代碼未評估爲True
。
下一頁
我試圖用t.IsSubclassOf(typeof(IEnumerable))
並沒有任何運氣無論是。
代碼
/// <summary>
/// Reflects an enumerable (not a list, bad name should be fixed later maybe?)
/// </summary>
/// <param name="o">The Object the property resides on.</param>
/// <param name="p">The Property We're reflecting on</param>
/// <param name="rla">The Attribute tagged to this property</param>
public void ReflectList(object o, PropertyInfo p, ReflectedListAttribute rla)
{
Type t = p.PropertyType;
//if (t.IsAssignableFrom(typeof(IEnumerable)))
if (t.IsSubclassOf(typeof(IEnumerable)))
{
IEnumerable e = p.GetValue(o, null) as IEnumerable;
int count = 0;
if (e != null)
{
foreach (object lo in e)
{
if (count >= rla.MaxRows)
break;
ReflectObject(lo, count);
count++;
}
}
}
}
意圖
我想基本上標籤列表我想用ReflectedListAttribute
,以反映通過,並呼籲有它的屬性此功能。 (已經工作)
一旦這個函數裏面,考慮到物業所在的對象,以及相關的PropertyInfo
,得到屬性的值,將其轉換爲一個IEnumerable(假設這是可能的),然後通過每個孩子迭代和用count變量調用ReflectObject(...)
。
哇,這是當你發現你很久以前嘲笑它並且不起作用時,當你回到舊代碼時會發生什麼。現在你提到它了,我沒有模糊的想法,我錯過了那個> _ <。謝謝 – Aren 2010-05-10 11:53:55