【问题标题】:Assembly.GetExportedTypes vs GetTypesAssembly.GetExportedTypes 与 GetTypes
【发布时间】:2017-05-09 08:13:13
【问题描述】:

Assembly.GetExportedTypes() 有什么作用?它与 Assembly.GetTypes() 有何不同

你能举例说明吗?

【问题讨论】:

    标签: c# .net


    【解决方案1】:

    GetExportedTypes() 不包括受保护/私有/内部类型。根据下面 Alexander 问题中链接的问题,即使通过 InternalsVisibleToAttribute 对程序集可见,内部类型也不包括在内。

    GetTypes() 包括所有类型。

    【讨论】:

    • 嵌套protected类型和嵌套protected internal类型,其声明类型是公共的,没有返回特别令人困惑。以TypeConverter+SimplePropertyDescriptor 为例:class MyClass : System.ComponentModel.TypeConverter { static void Main() { var exported = typeof(SimplePropertyDescriptor); Console.WriteLine(exported.IsVisible) /* writes False */; } }
    【解决方案2】:

    GetExportedTypes() 仅返回在相关程序集之外可见的类型。 “可见”是指公共类型和嵌套在其他公共类型中的公共类型。

    【讨论】:

    • 所以您的意思是使用 GetExportedTypes() 时返回所有公共类型? GetTypes() 那么做什么呢?
    【解决方案3】:

    关于InternalsVisibleToAttributethis question的一点评论。

    即使调用是由具有InternalsVisibleTo 属性的程序集进行的,GetExportedTypes() 也不会返回internal 类型。

    所以,GetExportedTypes() 只返回 public 类型。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多