, 여기에 .NET 2.0에서 근무 일반적으로 목록을 반영. 원래는 통과하는 속성에 IEnumerable
인터페이스가 지원되는지 감지하기 위해 원래 t.IsAssignableFrom(typeof(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