获取用户定义程序集下的所有类型 [英] Getting all types under a userdefined assembly

查看:23
本文介绍了获取用户定义程序集下的所有类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取在特定用户定义的命名空间下定义的所有类型

I am trying to get all the types defined under a particular userdefined namespace

Assembly.GetEntryAssembly().GetTypes().Where(t => t.Namespace == "namespace")


    <>c__DisplayClass3_0    
    <>c__DisplayClass4_0    
    <>c__DisplayClass6_0    
    <>c__DisplayClass2_0    
    <>c__DisplayClass2_1    
    <>c__DisplayClass2_2    
    <>c__DisplayClass2_3    
    <>c__DisplayClass2_4    
    <>c__DisplayClass2_5    
    <>c__DisplayClass2_6    
    <>c__DisplayClass2_7    
    <>c__DisplayClass2_8

我的问题为什么我会得到这些未在该命名空间下定义的额外类型?

My question Why am i getting these extra type which are not defined under that namespace?

如何选择属于用户定义类型的类型?

how do i select type that are user defined types?

有人向我解释这些是什么以及它们是如何在用户定义的命名空间下定义的.

some one explain me what are these and how they get defined under a userdefined namespace.

推荐答案

那些都是编译器生成的类型.C# 编译器生成类型以实现以下内容:

Those are all types generated by the compiler. The C# compiler generates types to implement things like:

  • Lambda 表达式和匿名方法
  • 迭代器块
  • 异步方法
  • 匿名类型

它们都应该有 CompilerGeneratedAttribute 应用于它们,因此您可以根据需要以这种方式将它们过滤掉:

All of them should have the CompilerGeneratedAttribute applied to them, so you can filter them out that way if you want:

var types = Assembly.GetEntryAssembly()
    .GetTypes()
    .Where(t => t.Namespace == "namespace")
    .Where(t => !t.GetTypeInfo().IsDefined(typeof(CompilerGeneratedAttribute), true));

这篇关于获取用户定义程序集下的所有类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆