初始化库大会上的负载负载、初始化、大会上

2023-09-02 21:34:57 作者:忘怀讲解

我有一个.NET库DLL,其作用类似于一个功能库。有一堆静态类型以及静态方法。

I have a .net library dll that acts like a functional library. There are a bunch of static types along with static methods.

有一些初始化code,我需要运行建立图书馆投入使用。

There is some initialization code that I need to run to set up the library ready for use.

当被加载的程序集是有办法,以确保特定的方法运行?喜欢的东西AppDomain.AssemblyLoad但是从大会本身自动调用。我在想,也许有类似的东西,可以使用的AssemblyAttribute?

When the assembly gets loaded is there a way to ensure that a particular method is run? Something like AppDomain.AssemblyLoad but called automatically from the assembly itself. I was thinking that maybe there is something like an AssemblyAttribute that could be used?

目前,我有这个初始code在静态构造函数,但因为这是许多切入点图书馆也不能保证,这种特定类型将被使用。

At the moment I have this initialization code in a static constructor but as this is a library with many entry points there is no guarantee that this particular type will be used.

谢谢!

推荐答案

为什么你需要的所有数据,之前的任意的它被加载时,而不是仅仅在第一类,需要它是用来?

Why do you need all the data to be loaded before any of it is used, rather than just when the first type which needs it is used?

我不相信有什么办法强迫的方法要在装配负荷运行,从组件内。你可以把一个静态构造函数中的每次的类型,但坦白说,我认为它只是更有意义,有一种类型的重presenting的数据,并给它提供接入 - 并把静态构造函数这种类型的孤单。 (如果你有单独的数据位,可以独立使用,可能产生不同的类型它们。)

I don't believe there's any way of forcing a method to be run on assembly load, from within the assembly. You could put a static constructor in every type, but frankly I think it just makes more sense to have a single type representing that data and providing access to it - and put a static constructor on that type alone. (If you've got separate bits of data which can be used independently, perhaps create separate types for them.)