问题描述
模块初始化是不是在C#或VB.NET直接使用CLR的一个特点。它们被命名为全局静态方法 .cctor
这是保证任何其他code之前运行在装配(类型初始化器,静态构造函数)被执行。我最近想在项目中使用它,然后的,但我想知道:
-
有什么办法来欺骗C#编译器为发光模块intializers?可用于任何属性(例如编译器生成,SpecialName)或其他诡计?
-
执行C#/ VB.NET不断放出这些初始化自己的某种目的?从我所看到它们是由一些互操作目的的管理C ++使用,但我找不到他们的任何引用被用于其他目的。任何想法?
没有,有没有办法发出他们在C#中,因为C#把一切都在一类/结构和模块的初始化必须是全球性的。
您将不得不使用不同的工具来写他们,preferably IL-汇编。
至于第二个问题,我不得不承认,我不知道,但我从来没有见过用C#的产生,我用ILDASM很多时候,所以我认为它不发射他们。
Module initializers are a feature of the CLR that are not directly available in C# or VB.NET. They are global static methods named .cctor
that are guaranteed to run before any other code (type initializers, static constructors) in an assembly are executed. I recently wanted to use this in a project and hacked together my own solution (console program/msbuild task) using Mono.Cecil, but I was wondering:
Is there any way to trick the C# compiler into emitting module intializers? Any attributes (e.g. CompilerGenerated, SpecialName) or other trickery that could be used?
Do the C# / VB.NET ever emit these initializers themselves for some purpose? From what I've seen they are used by managed C++ for some interop purposes, but I couldn't find any reference to them being used for other purposes. Any ideas?
No, there is no way to emit them in C#, because C# puts everything in a class/struct and module initializers need to be global.
You will have to use a different tool to write them, preferably IL-Assembler.
As for the second question, I have to admit that I don't know, but I have never seen any generated by C#, and I use ILDasm quite often, so I assume that it doesn't emit them.
这篇关于在C#模块初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!