获取所有可观测集合<;T>;基于基类型的对象的属性

本文关键字:于基 类型 属性 对象 gt 集合 lt 获取 可观 | 更新日期: 2023-09-27 18:30:00

我正在尝试使用反射获取对象的所有ObservableCollection属性。例如,我有一个Person类,它有ObservableCollecton和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);
}

以上内容没有返回任何内容。

有什么建议吗?

===>这是根据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;
}

获取所有可观测集合<;T>;基于基类型的对象的属性

您可以查找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`
}