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

眉间皱痕 提交于 2019-11-30 06:52:54
CharithJ

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

Have you looked into the PreApplicationStartMethodAttribute?

using System.Web;

[assembly: PreApplicationStartMethod(typeof(ClassLibrary.Startup), "Start")]

namespace ClassLibrary
{
    public class Startup
    {
        public static void Start()
        {
            // do some awesome stuff here!
        }
    }
}

More detail: http://dochoffiday.com/professional/simulate-application-start-in-class-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!