使用反射来获取静态类的列表 [英] Use reflection to get a list of static classes

查看:141
本文介绍了使用反射来获取静态类的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

许多问题都接近,但没有回答我的问题...

many questions are close, but none answers my problem...

我如何使用C#3.5反射来获取这是从一个程序集静态的所有类。我已经得到定义的所有类型,但没有IsStatic财产。计数0构造实在是太慢了,并没有擦出火花。

How do I use reflection in C# 3.5 to get all classes which are static from an assembly. I already get all Types defined, but there is no IsStatic property. Counting 0 constructors is really slow and did not work either.

任何提示或代码行? : - )

Any tips or a line of code? :-)

克里斯

推荐答案

下面是你如何获得类型从一个程序集:

Here is how you get types from an assembly:

http://msdn.microsoft.com/en-us/library/system.reflection.assembly.aspx

GetTypes方法

然后:

查找是抽象的,在同一时间密封的类

Look for the classes that are abstract and sealed at the same time.

http://dotneteers.net/blogs/divedeeper/archive/2008 /08/04/QueryingStaticClasses.aspx

在博客搜索我能找到的信息.NET CLR不知道静态类的想法,但允许同时使用抽象和密封型标志。这些标志也使用由CLR以优化其行为,例如密封标志用于像非虚函数密封类的呼叫的虚拟方法。
所以,要问,如果一个类型是静态的还是没有,你可以使用这个方法:

Searching in blogs I could find the information that .NET CLR does not know the idea of static classes, however allows using the abstract and sealed type flags simultaneously. These flags are also used by the CLR to optimize its behavior, for example the sealed flag is used call virtual methods of sealed class like non-virtuals. So, to ask if a type is static or not, you can use this method:

从下面的评论:

IEnumerable<Type> types = typeof(Foo).Assembly.GetTypes().Where
(t => t.IsClass && t.IsSealed && t.IsAbstract);

这篇关于使用反射来获取静态类的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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