【发布时间】:2014-11-11 20:31:03
【问题描述】:
我正在尝试使用反射获取对象的所有 ObservableCollection 属性。 例如我有一个 Person 类,它具有 ObservableCollection 和 ObservableCollection,PhoneModel 和 AddressModel 都继承自 ModelBaseclass。现在我有以下函数试图选择所有 ObservableCollection 属性。
/// <summary>
/// Get all the oversablecollection properties from the object
/// </summary>
/// <typeparam name="T">type to search for</typeparam>
/// <param name="model">object to return properties for</param>
public IList<ObservableCollection<T>> GetObservableCollections<T>(object model)
{
var type = model.GetType();
var result = new List<ObservableCollection<T>>();
foreach (var prop in type.GetProperties())
{
if (prop.PropertyType.IsAssignableFrom(typeof(ObservableCollection<T>)))
{
var get = prop.GetGetMethod();
if (!get.IsStatic && get.GetParameters().Length == 0) // skip indexed & static
{
var collection = (ObservableCollection<T>)get.Invoke(model, null);
if (collection != null)
result.Add(collection);
}
}
}
return result;
}
这行得通:
[TestMethod]
public void GetObservableCollections_HasOnePublicProperty_Return1()
{
var personWithPhones = GetValidPersonAndPhonesModel();
var collection = GetObservableCollections<PhoneModel>(personWithPhones);
Assert.IsTrue(collection.Count()==1);
}
我希望它能够正常工作,例如,不是针对特定模型而是针对 ModelBase 查找,例如,我希望所有 ObservableCollectionProperties 都用于地址和电话
[TestMethod]
public void GetObservableCollections_HasOnePublicProperty_Return1()
{
var personWithPhones = GetValidPersonAndPhonesModel();
var collection = GetObservableCollections<ModelBase>(personWithPhones);
Assert.IsTrue(collection.Count()==2);
}
上面没有返回任何东西。
有什么建议吗?
===> 这是彼得建议后的更新代码,但有一些转换错误:
public static IList<ObservableCollection<T>> GetObservableCollections<T>(object obj)
{
var type = obj.GetType();
IList<object> list = new List<object>();
IList<ObservableCollection<T>> result = new List<ObservableCollection<T>>();
foreach (var prop in type.GetProperties().Where(p=> p.PropertyType.IsGenericType))
{
var unclosedTyped = prop.PropertyType.GetGenericTypeDefinition();
if (unclosedTyped == typeof(ObservableCollection<>))
{
// you have an ObservableCollection<> property
var elementType = prop.PropertyType.GetGenericArguments().First();
if (typeof(ModelBase).IsAssignableFrom(elementType))
{
// you have indeed an ObservableCollection property
// with elements that inherit `ModelBase`
var get = prop.GetGetMethod();
if (!get.IsStatic && get.GetParameters().Length == 0) // skip indexed & static
{
var collection = get.Invoke(obj, null);
if (collection != null)
{
//list.Add(collection); // This works
result.Add((ObservableCollection<T>) collection);
}
}
}
}
}
return list;
}
【问题讨论】:
标签: c# reflection properties observablecollection