通过反射查找给定/动态类型的ObservableCollection [英] Finding an ObservableCollection for given/dynamic type via reflection
问题描述
我有一个带有几个ObservableCollections的Class,用于不同类型.现在,我想通过反射找到给定类型的正确Collection,因为我不想构建一个if-monster,每次添加另一个Collection时都必须对其进行更新.
I have a Class with several ObservableCollections for different types. Now, I want to find the correct Collection for a given type via reflection, because I don't want to build an if-monster which I have to update every time I add another Collection.
此方法是第一步:
public ObservableCollection<T> GetObservableCollectionForType<T>()
{
foreach (PropertyInfo info in this.GetType().GetProperties())
{
if (info.GetGetMethod() != null && info.PropertyType == typeof(ObservableCollection<T>))
return (ObservableCollection<T>)this.GetType().GetProperty(info.Name).GetValue(this, null);
}
return null;
}
现在,我需要第二种方法,该方法接受一个具体对象作为参数并找到正确的Collection.像这样:
Now, I need a second method, which accepts a concrete object as parameter and finds the correct Collection. Somehow like this:
public ObservableCollection<T> GetObservableCollectionFor(object sObject)
{
Type wantedType = sObject.GetType();
foreach (PropertyInfo info in this.GetType().GetProperties())
{
if (info.GetGetMethod() != null && info.PropertyType == ObservableCollection<wantedType>)
return this.GetType().GetProperty(info.Name).GetValue(this, null);
}
return null;
}
有什么想法可以实现这一目标吗?
Any ideas how to realize this?
更新:
可行的解决方案:
public object GetObservableCollectionFor(object sObject)
{
Type wantedType = sObject.GetType();
foreach (PropertyInfo info in this.GetType().GetProperties())
{
if (info.GetGetMethod() != null && info.PropertyType == typeof(ObservableCollection<>).MakeGenericType(new[]{wantedType}))
return this.GetType().GetProperty(info.Name).GetValue(this, null);
}
return null;
}
这将返回正确的集合作为对象.我仍然不知道如何转换为正确的泛型类型,但是转换为IList就足以添加和删除.
This will return the correct collection as object. I still don't know how to cast to the correct generic type, but casting to IList is enough for adding and removing.
推荐答案
比较属性的类型时,您似乎需要在ObservableCollection类型上添加对MakeGenericType()的调用.还没有测试过,但也许...
When comparing the type of the property, it looks like you need to add a call to MakeGenericType() on the ObservableCollection type. Haven't tested this but maybe something like...
public ObservableCollection<T> GetObservableCollectionFor(object sObject)
{
Type wantedType = sObject.GetType();
foreach (PropertyInfo info in this.GetType().GetProperties())
{
if (info.GetGetMethod() != null && info.PropertyType == typeof(ObservableCollection<>).MakeGenericType(new[]{Type.GetType(wantedType)})
return (ObservableCollection<T>)this.GetType().GetProperty(info.Name).GetValue(this, null);
}
return null;
}
修改 为避免使用值类型进行间接装箱,可以通过将参数类型从对象更改为类型T来改进上述方法定义.
Edit To avoid overhead boxing with value types, the above method definition could be improved by changing the parameter type from object to type T
public ObservableCollection<T> GetObservableCollectionFor(T sObject)
这篇关于通过反射查找给定/动态类型的ObservableCollection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!