sender as ObservableCollection<string> 为啥为null ?
查看下sender的传入值,确实是有内容的。
查看下sender的定义如下,确实是一个ObservableCollection<string>类型。
用即时窗口看下:
sender is List<string>
false
sender is ObservableCollection<string>
false
sender as List<string>
null
为啥子显示不是一个ObservableCollection<string>类型呢?
没办法,看下系统反射判断的这个sender定义的类型到底是啥:
sender.GetType()
{Name = "ObservableCollection`1" FullName = "System.Collections.ObjectModel.ObservableCollection`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]"}
Assembly: {System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089}
AssemblyQualifiedName: "System.Collections.ObjectModel.ObservableCollection`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
Attributes: Public | Serializable | BeforeFieldInit
BaseType: {Name = "Collection`1" FullName = "System.Collections.ObjectModel.Collection`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]"}
ContainsGenericParameters: false
CustomAttributes: Count = 3
DeclaredConstructors: {System.Reflection.ConstructorInfo[3]}
DeclaredEvents: {System.Reflection.EventInfo[3]}
DeclaredFields: {System.Reflection.FieldInfo[5]}
DeclaredMembers: {System.Reflection.MemberInfo[34]}
DeclaredMethods: {System.Reflection.MethodInfo[22]}
DeclaredNestedTypes: {System.Reflection.TypeInfo.<get_DeclaredNestedTypes>d__23}
DeclaredProperties: {System.Reflection.PropertyInfo[0]}
DeclaringMethod: '((System.RuntimeType)(sender.GetType())).DeclaringMethod' threw an exception of type 'System.InvalidOperationException'
DeclaringType: null
FullName: "System.Collections.ObjectModel.ObservableCollection`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]"
GUID: {eed6258a-e0b5-3222-9bfe-53563367be16}
GenericParameterAttributes: '((System.RuntimeType)(sender.GetType())).GenericParameterAttributes' threw an exception of type 'System.InvalidOperationException'
GenericParameterPosition: '((System.RuntimeType)(sender.GetType())).GenericParameterPosition' threw an exception of type 'System.InvalidOperationException'
GenericTypeArguments: {System.Type[1]}
GenericTypeParameters: {System.Type[0]}
HasElementType: false
ImplementedInterfaces: {System.Type[10]}
IsAbstract: false
IsAnsiClass: true
IsArray: false
IsAutoClass: false
IsAutoLayout: true
IsByRef: false
IsCOMObject: false
IsClass: true
IsConstructedGenericType: true
IsContextful: false
IsEnum: false
IsExplicitLayout: false
IsGenericParameter: false
IsGenericType: true
IsGenericTypeDefinition: false
IsImport: false
IsInterface: false
IsLayoutSequential: false
IsMarshalByRef: false
IsNested: false
IsNestedAssembly: false
IsNestedFamANDAssem: false
IsNestedFamORAssem: false
IsNestedFamily: false
IsNestedPrivate: false
IsNestedPublic: false
IsNotPublic: false
IsPointer: false
IsPrimitive: false
IsPublic: true
IsSealed: false
IsSecurityCritical: false
IsSecuritySafeCritical: false
IsSecurityTransparent: true
IsSerializable: true
IsSpecialName: false
IsUnicodeClass: false
IsValueType: false
IsVisible: true
MemberType: TypeInfo
MetadataToken: 33555385
Module (System.Reflection.MemberInfo): {System.dll}
Module: {System.dll}
Name: "ObservableCollection`1"
Namespace: "System.Collections.ObjectModel"
ReflectedType: null
StructLayoutAttribute: {System.Runtime.InteropServices.StructLayoutAttribute}
TypeHandle: {System.RuntimeTypeHandle}
TypeInitializer: null
UnderlyingSystemType: {Name = "ObservableCollection`1" FullName = "System.Collections.ObjectModel.ObservableCollection`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]"}
系统判断类型是System.Collections.ObjectModel.ObservableCollection
而鼠标指向 sender as ObservableCollection<string> 这一句,显示ObservableCollection的类型是 UIShell.OSGI.Collection.ObservableCollection!!
想不到吧! ObservableCollection在其它的类库中可能也有相同的类型。
一直以为系统的类型不可能在其它类型中再定义的。

