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

后端 未结 4 2043
陌清茗
陌清茗 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:21

    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

提交回复
热议问题