C# 获取所有可观察的收集<;T>;基于基类型的对象属性
我试图通过反射来获得一个物体的所有可观测收集属性。 e、 g.我有一个Person类,它有ObservaleCollecton和ObservaleCollection,PhoneModel和AddressModel都是从ModelBaseclass继承的。现在我有了下面的函数,它试图选择所有的ObservableCollection属性C# 获取所有可观察的收集<;T>;基于基类型的对象属性,c#,reflection,properties,observablecollection,C#,Reflection,Properties,Observablecollection,我试图通过反射来获得一个物体的所有可观测收集属性。 e、 g.我有一个Person类,它有ObservaleCollecton和ObservaleCollection,PhoneModel和AddressModel都是从ModelBaseclass继承的。现在我有了下面的函数,它试图选择所有的ObservableCollection属性 /// <summary> /// Get all the oversablecollection properties from the obje
/// <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;
}
//
///从对象获取所有OverTableCollection属性
///
///键入要搜索的内容
///对象返回其属性
公共IList GetObservableCollections(对象模型)
{
var type=model.GetType();
var result=新列表();
foreach(type.GetProperties()中的var prop)
{
if(prop.PropertyType.IsAssignableFrom(typeof(ObservableCollection)))
{
var get=prop.getMethod();
如果(!get.IsStatic&&get.GetParameters().Length==0)//跳过索引和静态
{
var collection=(ObservableCollection)get.Invoke(model,null);
if(集合!=null)
结果。添加(收集);
}
}
}
返回结果;
}
这项工作:
[TestMethod]
public void GetObservableCollections_HasOnePublicProperty_Return1()
{
var personWithPhones = GetValidPersonAndPhonesModel();
var collection = GetObservableCollections<PhoneModel>(personWithPhones);
Assert.IsTrue(collection.Count()==1);
}
[TestMethod]
public void getobservecollections\u HasOnePublicProperty\u Return1()
{
var personWithPhones=GetValidPersonAndPhonesModel();
var collection=GetObservableCollections(个人手机);
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);
}
[TestMethod]
public void getobservecollections\u HasOnePublicProperty\u Return1()
{
var personWithPhones=GetValidPersonAndPhonesModel();
var collection=GetObservableCollections(个人手机);
Assert.IsTrue(collection.Count()==2);
}
上面没有返回任何内容
有什么建议吗
=>这是Peter建议后更新的代码,但有一些铸造错误:
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;
}
公共静态IList GetObservableCollections(对象obj)
{
var type=obj.GetType();
IList list=新列表();
IList结果=新列表();
foreach(type.GetProperties()中的var prop,其中(p=>p.PropertyType.IsGenericType))
{
var unclosedTyped=prop.PropertyType.GetGenericTypeDefinition();
如果(未关闭类型==类型化(可观察集合))
{
//您有一个ObservableCollection属性
var elementType=prop.PropertyType.GetGenericArguments().First();
if(typeof(ModelBase).IsAssignableFrom(elementType))
{
//您确实有一个可观察的集合属性
//具有继承`ModelBase'的元素`
var get=prop.getMethod();
如果(!get.IsStatic&&get.GetParameters().Length==0)//跳过索引和静态
{
var collection=get.Invoke(obj,null);
if(集合!=null)
{
//list.Add(collection);//这很有效
结果。添加((ObservableCollection)集合);
}
}
}
}
}
退货清单;
}
您可以查找未关闭的ObservableCollection类型,而不是查看已关闭的泛型类型。大概是这样的:
var unclosedTyped = prop.PropertyType.GetGenericTypeDefinition();
if (unclosedTyped == typeof(ObservableCollection<>))
{
// you have an ObservableCollection<> property
}
嗨,彼得,谢谢你的回复。这似乎解决了这个问题,但在尝试将道具强制转换为ObservaleCollection时,我得到一个InvalidCastException,例如此行->var collection=(ObservaleCollection)get.Invoke(model,null)。。。。。我只是想知道这种方法是否有效?嗯,你在.NET3.5上吗?检查此线程:不,我在.NET4.5中。。。。我可以扮演一个效果很好的对象。。。但我不想那样我想。。。
var elementType = prop.PropertyType.GetGenericArguments().First();
if (typeof(ModelBase).IsAssignableFrom(elementType))
{
// you have indeed an ObservableCollection property
// with elements that inherit `ModelBase`
}