如何辨别 Type 是否是静态数组初始值设定项?
首先我要说的是,我正在消除这样的假设:静态数组初始值设定项由编译器转换为私有嵌套类,通常具有类似 __StaticArrayInitTypeSize=12
的名称。据我了解,已阅读 在这篇内容非常丰富的文章中,这些私有类是值类型,并且它们没有使用 CompilerGenerateAttribute
类进行标记。
我正在开发一个需要处理某些类型并忽略其他类型的项目。
我必须能够处理自定义结构类型,这些类型与生成的静态数组初始值设定项类一样,都是值类型。我必须忽略生成的静态数组初始值设定项类。我还必须忽略枚举和委托。
我使用 Linq 提取这些类,如下所示:
var typesToProcess = allTypes.Where(type => !type.IsEnum &&
!type.IsArray &&
!type.IsSubclassOf(typeof(Delegate)));
我相当确定 IsArray
属性不是我想象的那样。无论如何,生成的静态数组初始值设定项类仍然显示在 typesToProcess
Enumerable 中。
还有其他人处理过这个吗?如何辨别自定义结构和生成的静态数组初始值设定项类之间的区别?我可以通过将类型名称与 __StaticArrayInitTypeSize 进行字符串比较来破解它,但是有没有更干净的解决方案?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我自己刚刚使用 C# 4 编译器进行了尝试,得到了一个名为
{D1E23401-19BC-4B4E-8CC5-2C6DDEE7B97C}
的内部类,其中包含名为__StaticArrayInitTypeSize=12
的私有嵌套结构。该类包含一个名为
$$method0x6000001-1
的结构类型的内部静态字段。该字段本身是用CompilerGeneratedAttribute
修饰的。问题是所有这些都是特定于实现的。它可能在未来的版本中发生变化,或者也可能与早期版本不同。
任何包含
<
、>
或=
的成员名称都是“无法说出”的名称,将生成该名称由编译器生成,因此您可以将其视为一种隐式编译器生成(如果有任何用处的话)。 (不过,此类生成类型还有许多其他用途。)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.)