如何在程序集加载时在程序集中运行某些代码,即可以自定义.net程序集入口点? [英] How to run some code in an assembly when it being load, ie can I customize .net assembly entrypoint?

查看:86
本文介绍了如何在程序集加载时在程序集中运行某些代码,即可以自定义.net程序集入口点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Dll的入口点名为DllMain.我们可以在此函数中编写一些代码来执行一些操作 初始化作业,例如初始化库的私有堆.

Dll's  developed by VC++ have entry point named DllMain. We can write some code in this function to do some initialization job, like initialize library's private heap.

现在,我希望一些代码在.NET程序集加载时运行.  可能吗?

Now, I want some code to run at a .NET assembly load time.  Does it possible?

推荐答案

我相信DllMain仍将在程序集中或至少在Windows平台上运行.但是DllMain必须以本机代码实现,不能调用托管代码,并且会限制可移植性.

I believe DllMain will still work in an assembly, or at least on a windows platform. But the DllMain must be implemented in native code, will not be able to call into managed code and will constrain portability.

在托管代码中最接近DllMain的是模块构造函数(在'< module>'类上具有特殊名称'.cctor'的方法).我不了解VB,但C ++/CLI和ilasm支持生成模块构造函数,但C#却不支持.

The closest thing to DllMain in managed code is the module constructor (a method with the special name '.cctor' on the '<module>' class). I don't know about VB, but C++/CLI and ilasm support generating a module constructor but C# does not.

https://blogs.msdn.microsoft. com/junfeng/2005/11/19/module-initializer-aka-module-constructor/


这篇关于如何在程序集加载时在程序集中运行某些代码,即可以自定义.net程序集入口点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆