如何使用反射来获取在当前项目中的所有类? [英] How to get all classes in current project using reflection?
问题描述
我怎么能列出所有的课程在我目前的项目(集?)使用反射?谢谢。
How can I list all the classes in my current project(assembly?) using reflection? thanks.
推荐答案
给定一个实例 A
System.Reflection.Assembly的,你可以用得到的所有类型的组件
Given an instance a
of System.Reflection.Assembly, you can get all types in the assembly using:
var allTypes = a.GetTypes();
这会给你所有类型的,公开的,内部的和私有的。
This will give you all types, public, internal and private.
如果您只想公共类型,你可以使用:
If you want only the public types, you can use:
var publicTypes = a.GetExportedTypes();
如果您正在从大会本身在这个code,可以使用得到组装
If you are running this code from within the Assembly itself, you can get the assembly using
var a = Assembly.GetExecutingAssembly();
GetTypes和GetExportedTypes会给你所有类型(结构,类,枚举,接口等),所以如果你想只有类则必须过滤
GetTypes and GetExportedTypes will give you all types (structs, classes, enums, interfaces etc.) so if you want only classes you will have to filter
var classes = a.GetExportedTypes().Where(t => t.IsClass);
这篇关于如何使用反射来获取在当前项目中的所有类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!