我得到一個類型,其全名是:爲什麼我的測試「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