Is there an equivalent of Application_Start for a class library in c#

后端 未结 4 2034
陌清茗
陌清茗 2020-12-30 02:29

I would like to execute certain code in a class library when it is instantiated from another assembly. Is there an entry point or bootstrap for a class library? I thought t

4条回答
  •  不思量自难忘°
    2020-12-30 03:03

    AppDomain.AssemblyLoad Event which occurs when an assembly is loaded. Probably that can be used to call an initialize method in your class library.

    public static void Main() 
    {
        AppDomain currentDomain = AppDomain.CurrentDomain;
        currentDomain.AssemblyLoad += new AssemblyLoadEventHandler(MyAssemblyLoadEventHandler);
    }
    
    static void MyAssemblyLoadEventHandler(object sender, AssemblyLoadEventArgs args) 
    {
          Console.WriteLine("ASSEMBLY LOADED: " + args.LoadedAssembly.FullName);
          //If this is the assembly that you want to call an initialize method..
    }
    

    Below are two similar threads

    how to write class lib's assembly load/init event handler

    .Net: Running code when assembly is loaded

提交回复
热议问题