在用户定义的程序集下获取所有类型 [英] Getting all types under a userdefined assembly
本文介绍了在用户定义的程序集下获取所有类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试获取在特定的用户定义名称空间下定义的所有类型
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表达式和匿名方法
- 迭代器块
- 异步方法
- 匿名类型
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屋!
查看全文