var types=from m in System.Reflection.Assembly.Load("System").GetTypes()
where m.IsClass
where // something to check whether or not the type is a static class.
select m;
我希望你能帮我筛选掉结果中的任何静态类。
var types=from m in System.Reflection.Assembly.Load("System").GetTypes()
where m.IsClass
where // something to check whether or not the type is a static class.
select m;
var types = from m in System.Reflection.Assembly.Load("System").GetTypes()
where m.IsClass && m.IsAbstract && m.IsSealed
select m;
来自这个线程。
编辑:检查m.IsSealed
无论你做什么,都会基于一种启发式方法 - 在IL级别上没有特定的“这个类是静态的”。而且,不能保证C#和VB编译器在未来版本中如何实现静态/模块。
嗯,静态类将没有公共构造函数,并且将被封闭,因此以下内容可能已足够:
var types=from m in System.Reflection.Assembly.GetExecutingAssembly().GetTypes()
where m.IsClass && (!m.IsSealed || m.GetConstructors().Any())
select m;
你需要检查类是否为Sealed和Abstract。
CLR不知道静态类,但它支持密封的抽象类,即使你不能显式地编译它们,静态类也会被编译成密封的抽象类。