在c#中是否有类似于Application_Start的类库

当从另一个程序集实例化时,我想在类库中执行某些代码。 是否有类库的入口点或引导程序? 我认为静态方法Main可以做到这一点,但我错了。 应用程序可能是配置和实例化记录器单例,未处理的exception处理程序等。

您是否查看了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

AppDomain.AssemblyLoad加载程序集时发生的事件 。 可能用于在类库中调用initialize方法。

 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.. } 

下面是两个相似的主题

如何编写类lib的程序集load / init事件处理程序

.Net:加载程序集时运行代码