static-constructor

Emulating static constructors for templated classes

最后都变了- 提交于 2021-02-07 09:12:28
问题 I would like to have a templated class with a static data member, and initialize it by emulating a "static constructor." For a non-templated class, this has already been answered (see static constructors in C++? I need to initialize private static objects and What is a static constructor?). However, none of the answers seem to work for a templated class. The following is an example that tries to adapt the "static constructor" idiom from the previous answers to a templated class. (Note that

Emulating static constructors for templated classes

不问归期 提交于 2021-02-07 09:11:32
问题 I would like to have a templated class with a static data member, and initialize it by emulating a "static constructor." For a non-templated class, this has already been answered (see static constructors in C++? I need to initialize private static objects and What is a static constructor?). However, none of the answers seem to work for a templated class. The following is an example that tries to adapt the "static constructor" idiom from the previous answers to a templated class. (Note that

What is the earliest entrypoint that the CLR calls before calling any method in an assembly?

与世无争的帅哥 提交于 2020-01-23 00:20:08
问题 In the past years I've occasionally been wondering what equivalent of the (in)famous DLL_PROCESS_ATTACH was available in the .NET world. Any documentation I have says, slightly simplified, that the earliest entry point to a class is the static constructor (cctor), but you cannot influence when it is called, nor can you define one cctor that's guaranteed to be called prior to any other cctor or field initializer, hack, it may not even be called at all if the class is never used. So, if you

Why aren't all static constructors called in C# (i.e. those of the parent classes)?

ⅰ亾dé卋堺 提交于 2020-01-01 09:58:27
问题 I have three classes, Base , Derived and Final . Derived derives from Base and Final derives from Derived . All three classes have a static constructor. Class Derived as a public static method called Setup . When I call Final.Setup , I expect that all three static constructors get executed, but only the one in Derived gets run. Here is the sample source code: abstract class Base { static Base() { System.Console.WriteLine ("Base"); } } abstract class Derived : Base { static Derived() { System

Why aren't all static constructors called in C# (i.e. those of the parent classes)?

六眼飞鱼酱① 提交于 2020-01-01 09:57:09
问题 I have three classes, Base , Derived and Final . Derived derives from Base and Final derives from Derived . All three classes have a static constructor. Class Derived as a public static method called Setup . When I call Final.Setup , I expect that all three static constructors get executed, but only the one in Derived gets run. Here is the sample source code: abstract class Base { static Base() { System.Console.WriteLine ("Base"); } } abstract class Derived : Base { static Derived() { System

Explicitly call static constructor

我们两清 提交于 2019-12-31 17:49:13
问题 I want to write unit test for below class. If name is other than "MyEntity" then mgr should be blank. Negative Unit test Using Manager private accessor I want to change name to "Test" so that mgr should be null. And then will verify the mgr value. To achieve this, I want to explicitly call the static constructor but when I call the static constructor using Manager_Accessor.name = "Test" typeof(Manager).TypeInitializer.Invoke(null, null); name is always set to "MyEntity" how to set name to

Is a Java static block equivalent to a C# static constructor?

删除回忆录丶 提交于 2019-12-30 03:05:07
问题 What is the real difference between a C# static constructor and a Java static block? They both must be parameterless. They are both called only once, when the related class is first used. Am I missing something, or are they the same thing, just with different names? 回答1: They are equivalent, except that a C# class can only have one static constructor (plus static field initializers). Also, in C#, a static constructor will apply the beforefieldinit flag. 回答2: They look the same, the following

Is a Java static block equivalent to a C# static constructor?

孤者浪人 提交于 2019-12-30 03:05:06
问题 What is the real difference between a C# static constructor and a Java static block? They both must be parameterless. They are both called only once, when the related class is first used. Am I missing something, or are they the same thing, just with different names? 回答1: They are equivalent, except that a C# class can only have one static constructor (plus static field initializers). Also, in C#, a static constructor will apply the beforefieldinit flag. 回答2: They look the same, the following

What's the best way to ensure a base class's static constructor is called?

陌路散爱 提交于 2019-12-28 03:41:25
问题 The documentation on static constructors in C# says: A static constructor is used to initialize any static data, or to perform a particular action that needs performed once only. It is called automatically before the first instance is created or any static members are referenced . That last part (about when it is automatically called) threw me for a loop; until reading that part I thought that by simply accessing a class in any way , I could be sure that its base class's static constructor

Factory pattern with static registration

帅比萌擦擦* 提交于 2019-12-24 15:55:18
问题 I'm having a problem when trying to register my types using their static constructors, with the following factory: public class Factory<T> { public static Factory<T> Instance { get { return _instance; } } private static Factory<T> _instance = new Factory<T>(); private Factory() { } static Factory() { } static Dictionary<string, Type> _registeredType = new Dictionary<string, Type>(); public void Register(string id, T obj) { if (obj.GetType().IsAbstract || obj.GetType().IsInterface) throw new