【发布时间】:2011-12-21 09:27:00
【问题描述】:
我有一个全名是:
"System.Collections.ObjectModel.ObservableCollection`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]"
问题是我想测试我的 Type 是否是字符串的 ObservableCollection(在当前情况下是)。所以这是我的代码:
if (propertyType.GetType() == typeof(ObservableCollection<string>))
但它似乎失败了,我不明白为什么:/
我有这段代码可以工作:
if (propertyType.Namespace == "System.Collections.ObjectModel" && propertyType.Name == "ObservableCollection`1")
{
//We are dealing with an ObservableCollection
var args = propertyType.GetGenericArguments();
if (args.Count() != 0 && args[0] == typeof(string))
{
//MyCode for ObservableCollection<string>
}
}
但我觉得这不是最佳选择,并且考虑到我必须处理其他类型(int、bool 等...)的其他集合(IEnumerable、List 等...),这不合适:(
【问题讨论】:
-
您能否在问题中添加属性声明以及如何填充 propertyType 变量?因为它应该可以工作。
-
我正在使用“typeof”进行测试,所以我不必在我的 Type 上使用“GetType()”:(
-
你确定这就是你的全部代码吗?这个 Observable 集合是一个类的属性吗?
标签: c# generics observablecollection typeof