16

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

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.

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!!

Ondrej Janacek
  • 12,486
  • 14
  • 59
  • 93
Levitikon
  • 7,749
  • 9
  • 56
  • 74
  • How do you believe such feature exist? Any reference? – Sriram Sakthivel Nov 04 '13 at 19:35
  • That's what I'm trying to figure out. I've come across it twice in the last year and thought it was really cool. Now for the life of me I can't recall or google this. – Levitikon Nov 04 '13 at 19:36
  • but are you sure this exist? I don't think so it exit(atleast in c#). I did some research some time back. Can't get anything. – Sriram Sakthivel Nov 04 '13 at 19:38
  • Be ware - this can cause problems with loader locks. I think that may be part of the reason it is not widely supported. You are better off writing some simple reflection code to look for a class with an attribute and instantiate that in a model similar to what the nunit framework does. – Neil Nov 04 '13 at 19:44
  • Found it! PreApplicationStartMethod! Thanks all! – Levitikon Nov 04 '13 at 19:57
  • 1
    Good to read you've found it, you might want to add it as an answer so we can vote for it. – Jos Vinke Nov 04 '13 at 19:58
  • Since it turns out this was actually an ASP.NET question and that the answer was not module initializers I'm voting to reopen this. – J... Nov 05 '13 at 11:25

5 Answers5

8

Turns out I was looking for PreApplicationStartMethod! Thanks all!

Levitikon
  • 7,749
  • 9
  • 56
  • 74
  • 3
    If this is specific for ASP.NET then I'll suggest you to add appropriate tag and some text in your question. That will hep google to find this page(is what I believe). – Sriram Sakthivel Nov 04 '13 at 20:04
  • The link for PreApplicationStartMethod above no longer exists. However, a simple google search will yield some good results. – David Nov 03 '20 at 13:07
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.

Some links with further information and research:

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

Module initializers in C#

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

Community
  • 1
  • 1
Ondrej Janacek
  • 12,486
  • 14
  • 59
  • 93
  • 2
    This is basically Hans's answer from here : http://stackoverflow.com/questions/505237/net-running-code-when-assembly-is-loaded – J... Nov 04 '13 at 19:47
  • Turns out I was looking for PreApplicationStartMethod! Thanks though! – Levitikon Nov 04 '13 at 19:58
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.

From the documentation:

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

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();
}
Jos Vinke
  • 2,704
  • 3
  • 26
  • 45
2

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

Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
1

Are you looking for Module Initializers?

Shiva
  • 20,575
  • 14
  • 82
  • 112