净:运行code时加载程序集 [英] .Net: Running code when assembly is loaded
问题描述
是否有可能一个程序集加载时运行一些code,而不做具体的装载code什么?我所寻找的是像一个类型的静态构造函数。
Is it possible to run some code when an assembly is loaded, without doing anything specific in the loading code? What I am looking for is like a static constructor on a type.
例如:
大会A不知道组件B,而B不知道A.大会A需要知道一些事情关于B如果装B的。当组件B由运行时(引用,或明确)加载,我要一张code(静态方法或属性)将要执行的调用组件A的方法。
Assembly A does not know about Assembly B, but B does know about A. Assembly A needs to know certain things about B if B is loaded. When Assembly B is loaded by the runtime (referenced, or explicit), I want a piece of code (static method or attribute) to be executed that calls a method in Assembly A.
此问题的根本原因是,当序列化输入一个包含来自B型不知道在编译时的接口用于未知类型所遇到。
The root cause of this problem is unknown types being encountered when serializing a type in A that contains types from B not known at compile time as interfaces are used.
推荐答案
在CLR支持模块初始化的。你必须破解C ++ / CLI code或ilasm.exe使用它们。
The CLR supports module initializers. You'd have to hack C++/CLI code or ilasm.exe to use them.
这篇关于净:运行code时加载程序集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!