为啥sender as ObservableCollection会为null ?



sender as ObservableCollection<string> 为啥为null ? 


查看下sender的传入值,确实是有内容的。

image.png


查看下sender的定义如下,确实是一个ObservableCollection<string>类型。

image.png



用即时窗口看下:

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!!



image.png


想不到吧! ObservableCollection在其它的类库中可能也有相同的类型。

一直以为系统的类型不可能在其它类型中再定义的。




本文出自勇哥的网站《少有人走的路》wwww.skcircle.com,转载请注明出处!讨论可扫码加群:

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

会员中心
搜索
«    2024年4月    »
1234567
891011121314
15161718192021
22232425262728
2930
网站分类
标签列表
最新留言
    热门文章 | 热评文章 | 随机文章
文章归档
友情链接
  • 订阅本站的 RSS 2.0 新闻聚合
  • 扫描加本站机器视觉QQ群,验证答案为:halcon勇哥的机器视觉
  • 点击查阅微信群二维码
  • 扫描加勇哥的非标自动化群,验证答案:C#/C++/VB勇哥的非标自动化群
  • 扫描加站长微信:站长微信:abc496103864
  • 扫描加站长QQ:
  • 扫描赞赏本站:
  • 留言板:

Powered By Z-BlogPHP 1.7.2

Copyright Your skcircle.com Rights Reserved.

鄂ICP备18008319号


站长QQ:496103864 微信:abc496103864