在启动时自动运行引用程序集上的代码?这是什么叫什么?

时间:2021-12-07 03:06:18

In .NET, there is something that can automatically run a piece of code in a referenced assembly when the assembly is loaded.

在。net中,当加载程序集时,可以自动地在引用程序集中运行一段代码。

For example, you can have a class decorated with a sort of attribute that lives in project Foo(A Class Library). And project Bar(A Web App) simply references project Foo. When Bar loads, that decorated code in Foo gets run somehow. I believe this is a newer feature.

例如,您可以拥有一个以项目Foo(一个类库)的属性来装饰的类。项目栏(一个Web应用程序)只是引用项目Foo。当Bar加载时,Foo中的修饰代码会以某种方式运行。我认为这是一个新特性。

Can someone tell what this feature is called??

有人知道这个功能叫什么吗?

Update: Thanks Shiva! Not Module Initialize. Although it lead me to the right answer. PreApplicationStartMethod and it's supported in .NET! Thanks all!!

更新:谢谢湿婆!模块初始化。虽然这让我找到了正确的答案。PreApplicationStartMethod并且在。net中支持它!谢谢! !

5 个解决方案

#1


7  

Turns out I was looking for PreApplicationStartMethod! Thanks all!

原来我在找PreApplicationStartMethod!感谢所有!

#2


3  

You might have also have a look at Fody. Fody is "an Extensible tool for weaving .net assemblies" which you can install as a nuget package. There is an add-in for fody called Module Initializers. Which under the hood uses the, in other answers already mentioned module initializers, but takes away the plumping.

你也可以看看Fody。Fody是“用于编织.net程序集的可扩展工具”,您可以将其作为nuget包安装。有一个用于fody的外接程序称为模块初始化器。在引擎盖下面使用,在其他答案中已经提到的模块初始化器,但是去掉了plumping。

From the documentation:

从文档:

What it does: Finds a class, in the target assembly, named ModuleInitializer with the following form:

它的作用:在目标程序集中找到一个名为ModuleInitializer的类,其形式如下:

public static class ModuleInitializer
{
    public static void Initialize()
    {
        //Init code
    }
}

It then Injects the following code into the module initializer of the target assembly. This code will be called when the assembly is loaded into memory

然后将以下代码注入目标程序集的模块初始化器。当程序集加载到内存中时,将调用此代码

static <Module>()
{
    ModuleInitializer.Initialize();
}

#3


3  

Do you, by chance, mean Module Initializer? They are capable of things you describe, but it seems that they are not supported in C#. They are a part of CLR yes, but not a part of C# language itself.

您是否碰巧是指模块初始化器?它们能够描述您描述的内容,但是看起来它们在c#中不受支持。它们是CLR的一部分,但不是c#语言本身的一部分。

Some links with further information and research:

与进一步信息和研究的一些联系:

http://blogs.msdn.com/b/junfeng/archive/2005/11/19/494914.aspx

http://blogs.msdn.com/b/junfeng/archive/2005/11/19/494914.aspx

Module initializers in C#

在c#模块初始化

.Net: Running code when assembly is loaded - thanks to J... for pointing out this link

.Net:加载程序集时运行代码-感谢J…指出这个联系

#4


2  

If am not wrong then you're looking for "Module Initializer". Check this out

如果没有错误,那么您正在寻找“模块初始化器”。看看这个

#5


1  

Are you looking for Module Initializers?

您正在寻找模块初始化程序吗?

#1


7  

Turns out I was looking for PreApplicationStartMethod! Thanks all!

原来我在找PreApplicationStartMethod!感谢所有!

#2


3  

You might have also have a look at Fody. Fody is "an Extensible tool for weaving .net assemblies" which you can install as a nuget package. There is an add-in for fody called Module Initializers. Which under the hood uses the, in other answers already mentioned module initializers, but takes away the plumping.

你也可以看看Fody。Fody是“用于编织.net程序集的可扩展工具”,您可以将其作为nuget包安装。有一个用于fody的外接程序称为模块初始化器。在引擎盖下面使用,在其他答案中已经提到的模块初始化器,但是去掉了plumping。

From the documentation:

从文档:

What it does: Finds a class, in the target assembly, named ModuleInitializer with the following form:

它的作用:在目标程序集中找到一个名为ModuleInitializer的类,其形式如下:

public static class ModuleInitializer
{
    public static void Initialize()
    {
        //Init code
    }
}

It then Injects the following code into the module initializer of the target assembly. This code will be called when the assembly is loaded into memory

然后将以下代码注入目标程序集的模块初始化器。当程序集加载到内存中时,将调用此代码

static <Module>()
{
    ModuleInitializer.Initialize();
}

#3


3  

Do you, by chance, mean Module Initializer? They are capable of things you describe, but it seems that they are not supported in C#. They are a part of CLR yes, but not a part of C# language itself.

您是否碰巧是指模块初始化器?它们能够描述您描述的内容,但是看起来它们在c#中不受支持。它们是CLR的一部分,但不是c#语言本身的一部分。

Some links with further information and research:

与进一步信息和研究的一些联系:

http://blogs.msdn.com/b/junfeng/archive/2005/11/19/494914.aspx

http://blogs.msdn.com/b/junfeng/archive/2005/11/19/494914.aspx

Module initializers in C#

在c#模块初始化

.Net: Running code when assembly is loaded - thanks to J... for pointing out this link

.Net:加载程序集时运行代码-感谢J…指出这个联系

#4


2  

If am not wrong then you're looking for "Module Initializer". Check this out

如果没有错误,那么您正在寻找“模块初始化器”。看看这个

#5


1  

Are you looking for Module Initializers?

您正在寻找模块初始化程序吗?