是否有对的Application_Start在C#中的类库等效类库、Application_Start

2023-09-03 07:09:10 作者:纣王虽暴,却爱妲己

我想在类库执行某些code,当它从另一个组件实例化。是否有一个入口点或引导的一个类库?我认为,一个静态方法主要会做的伎俩,但我错了。 申请这​​可能是配置和实例化一个记录单,未处理的异常处理程序等。

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 that a static method Main would do the trick but I was wrong. Applications for this might be configuring and instantiating a logger singleton, unhandled exception handler, etc.

推荐答案

你有没有调查过的 preApplicationStartMethodAttribute ?

using System.Web;

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

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

更多细节:的http://dochoffiday.com/professional/simulate-application-start-in-class-library