我试图使用反射获取对象的所有ObservableCollection属性。 例如我有一个具有ObservableCollecton和ObservableCollection的Person类,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);
}
上述内容不会返回任何内容。
有什么建议吗?
===&GT;这是彼得建议之后的更新代码,但有一些投射错误:
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;
}
答案 0 :(得分:2)
您可以查找未关闭的类型的ObservableCollection<>
,而不是查看已关闭的泛型类型。像这样:
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`
}