C#:列出汇编中的所有类

发布于 2024-08-03 01:52:19 字数 64 浏览 1 评论 0原文

我想输出(以编程方式 - C#)程序集中所有类的列表。

有任何提示或示例代码如何执行此操作?反射?

I'd like to output (programmatically - C#) a list of all classes in my assembly.

Any hints or sample code how to do this? Reflection?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倾城泪 2024-08-10 01:52:19

使用 Assembly.GetTypes。例如:

Assembly mscorlib = typeof(string).Assembly;
foreach (Type type in mscorlib.GetTypes())
{
    Console.WriteLine(type.FullName);
}

Use Assembly.GetTypes. For example:

Assembly mscorlib = typeof(string).Assembly;
foreach (Type type in mscorlib.GetTypes())
{
    Console.WriteLine(type.FullName);
}
绿萝 2024-08-10 01:52:19

我想补充一下乔恩的例子。要获取对您自己的程序集的引用,您可以使用:

Assembly myAssembly = Assembly.GetExecutingAssembly();

System.Reflection 命名空间。

如果要检查没有引用的程序集,可以使用以下任一方法:

Assembly assembly = Assembly.ReflectionOnlyLoad(fullAssemblyName);
Assembly assembly = Assembly.ReflectionOnlyLoadFrom(fileName);

如果您打算在找到类型后实例化该类型:

Assembly assembly = Assembly.Load(fullAssemblyName);
Assembly assembly = Assembly.LoadFrom(fileName);

请参阅 汇编类文档了解更多信息。

一旦获得了 Assembly 对象的引用,您就可以像 Jon 已经演示的那样使用 assembly.GetTypes()

I'd just like to add to Jon's example. To get a reference to your own assembly, you can use:

Assembly myAssembly = Assembly.GetExecutingAssembly();

System.Reflection namespace.

If you want to examine an assembly that you have no reference to, you can use either of these:

Assembly assembly = Assembly.ReflectionOnlyLoad(fullAssemblyName);
Assembly assembly = Assembly.ReflectionOnlyLoadFrom(fileName);

If you intend to instantiate your type once you've found it:

Assembly assembly = Assembly.Load(fullAssemblyName);
Assembly assembly = Assembly.LoadFrom(fileName);

See the Assembly class documentation for more information.

Once you have the reference to the Assembly object, you can use assembly.GetTypes() like Jon already demonstrated.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文