I’ll start by saying that I’m working off the assumption that static array initializers are turned into private nested classes by the compiler, usually with names like __StaticArrayInitTypeSize=12. As I understand it, having read this extremely informative article, these private classes are value types, and they aren’t tagged with the CompilerGeneratedAttribute class.
I’m working on a project that needs to process certain types and ignore others.
I have to be able to process custom struct types, which, like the generated static array initializer classes, are value types. I must ignore the generated static array initializer classes. I also must ignore enumerations and delegates.
I’m pulling these classes with Linq, like so:
var typesToProcess = allTypes.Where(type => !type.IsEnum &&
!type.IsArray &&
!type.IsSubclassOf(typeof(Delegate)));
I’m fairly sure that the IsArray property isn’t what I think it is. At any rate, the generated static array initializer class still shows up in the typesToProcess Enumerable.
Has anyone else dealt with this? How can I discern the difference between a custom struct and a generated static array initializer class? I could hack it by doing a string comparison of the type name against __StaticArrayInitTypeSize, but is there a cleaner solution?
Well, having just tried it myself with the C# 4 compiler, I got an internal class called
<PrivateImplementationDetails>{D1E23401-19BC-4B4E-8CC5-2C6DDEE7B97C}containing a private nested struct called__StaticArrayInitTypeSize=12.The class contained an internal static field of the struct type called
$$method0x6000001-1. The field itself was decorated withCompilerGeneratedAttribute.The problem is that all of this is implementation-specific. It could change in future releases, or it could be different from earlier releases too.
Any member name containing
<,>or=is an “unspeakable” name which will have been generated by the compiler, so you can view that as a sort of implicitCompilerGenerated, if that’s any use. (There are any number of other uses for such generated types though.)