many questions are close, but none answers my problem...
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? :-)
Chris
Here is how you get types from an assembly:
http://msdn.microsoft.com/en-us/library/system.reflection.assembly.aspx
GetTypes Method
Then:
Look for the classes that are abstract and sealed at the same time.
http://dotneteers.net/blogs/divedeeper/archive/2008/08/04/QueryingStaticClasses.aspx
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:
From the comment below:
IEnumerable<Type> types = typeof(Foo).Assembly.GetTypes().Where
(t => t.IsClass && t.IsSealed && t.IsAbstract);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With