我得到一个类型,其全名是:为什么我的测试“propType == typeof(ObservableCollection <string>)”失败?
"System.Collections.ObjectModel.ObservableCollection`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]"
的事情是,我想测试,如果我的类型为字符串的一个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,etcetc ...)的其他类型的集合(intn,bool,etcetc ...),这不太适合:(
你能在你的问题中添加属性声明,以及如何填写属性类型变量?因为它应该工作。 – Eilistraee 2011-12-21 09:32:28
我正在做一个“typeof”的测试,所以我不需要在我的类型上使用“GetType()”:( – 2011-12-21 09:35:51
你确定这就是你的所有代码?这个Observable集合是一个类的peroprty吗? – ChrisBD 2011-12-21 09:36:06