在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:加载程序集时运行代码
上述就是C#学习教程:在c#中是否有类似于Application_Start的类库分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1009717.html