static-constructor

c# static constructor not called from derived class

一笑奈何 提交于 2019-12-06 02:25:50
问题 class Bus<T> { static Bus() { foreach(FieldInfo fi in typeof(T).GetFields()) { if(fi.FieldType == typeof(Argument)) { fi.SetValue(typeof(T), new Argument("busyname", "busyvalue")); } } } } class Buss : Bus<Buss> { public static Argument field; } Any ideas how to make this work so that a reference to the static field in Buss triggers the static constructor in Bus? 回答1: The fact that this matters to you probably means that you are using static constructors wrong. With that in mind, you could

How to trigger a static constructor

余生颓废 提交于 2019-12-06 01:57:29
问题 code: class Base<T,U> where T:Base<T,U>,new() where U :class { protected static U _val = null; internal static void ShowValue() { if(_val == null)new T(); //Without this line, it won't work as expected Console.WriteLine (_val); } internal static void Virtual() { Console.WriteLine ("Base"); } } class Deriv :Base<Deriv,string> { static Deriv() { _val = "some string value"; } internal static new void Virtual () { Console.WriteLine ("Deriv"); } } public static void Main (string[] args) { Deriv

c++/cli static constructor of derived class is not called

限于喜欢 提交于 2019-12-05 22:24:22
As described in another SO post of me I saw a strange behaviour of my application after moving from VS 2008 (.net 3.5) to VS 2013 (and using .net 4.0, not 4.5). I found that the static constructor (cctor) of a class was not called any more. Therefore I broke the application down into a small test program: DLLs testAssembly_2-0 and testAssembly_4-0 (similar content; testAssembly_4-0 has names with 40 instead of 20 ) namespace testAssembly_20 { public ref class Class20 { public: Class20 () { Console::WriteLine (__FUNCTION__"()"); } static Class20 () { Console::WriteLine (__FUNCTION__"()" + " ms

Is RunClassConstructor guaranteed to run a type's static constructor only once?

穿精又带淫゛_ 提交于 2019-12-05 03:29:53
I'm calling the static ctor of a class using this code: Type type; System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(type.TypeHandle); Can this cause the cctor to be run twice? Thomas Levesque RunClassConstructor runs the static constructor only once, even if you call it twice. Just try ;) using System.Runtime.CompilerServices; ... void Main() { RuntimeHelpers.RunClassConstructor(typeof(Foo).TypeHandle); RuntimeHelpers.RunClassConstructor(typeof(Foo).TypeHandle); Foo.Bar(); } class Foo { static Foo() { Console.WriteLine("Foo"); } public static void Bar() { Console.WriteLine(

Forcing class load

空扰寡人 提交于 2019-12-04 17:21:02
问题 Is there a way in C# or .net IL to force a class that has a type initializer (static constructor) to load itself, without accessing any of its parameters? Assuming I've got the class public static class LogInitialization { static LogInitialization() { System.Console.WriteLine("Initialized"); } } Is there a way to get this line to print? Note that the class is static so I can't instantiate it to force initialization, and it has no public members so I can't access them to start it. 回答1:

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

本秂侑毒 提交于 2019-12-04 10:48:51
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 want to guarantee something's initialized before any method of your assembly is called and you don't want

How to trigger a static constructor

左心房为你撑大大i 提交于 2019-12-04 07:39:56
code: class Base<T,U> where T:Base<T,U>,new() where U :class { protected static U _val = null; internal static void ShowValue() { if(_val == null)new T(); //Without this line, it won't work as expected Console.WriteLine (_val); } internal static void Virtual() { Console.WriteLine ("Base"); } } class Deriv :Base<Deriv,string> { static Deriv() { _val = "some string value"; } internal static new void Virtual () { Console.WriteLine ("Deriv"); } } public static void Main (string[] args) { Deriv.ShowValue(); Deriv.Virtual(); } Thanks to the generics of .NET, I can create a bunch of specific classes

c# static constructor not called from derived class

China☆狼群 提交于 2019-12-04 07:36:47
class Bus<T> { static Bus() { foreach(FieldInfo fi in typeof(T).GetFields()) { if(fi.FieldType == typeof(Argument)) { fi.SetValue(typeof(T), new Argument("busyname", "busyvalue")); } } } } class Buss : Bus<Buss> { public static Argument field; } Any ideas how to make this work so that a reference to the static field in Buss triggers the static constructor in Bus? The fact that this matters to you probably means that you are using static constructors wrong. With that in mind, you could make a static constructor in Buss that manually invokes the static constructor in Bus . Note that it's not

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

﹥>﹥吖頭↗ 提交于 2019-12-04 05:48:39
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.Console.WriteLine ("Derived"); } public static void Setup() { System.Console.WriteLine ("Setup"); } }

Public constructor and static constructor

丶灬走出姿态 提交于 2019-12-03 15:39:20
问题 I am reading a code in C# that uses two constructors. One is static and the other is public. What is the difference between these two constructors? And for what we have to use static constructors? 回答1: static and public are orthogonal concepts (i.e. they don’t have anything to do with each other). public simply means that users of the class can call that constructor (as opposed to, say, private ). static means that the method (in this case the constructor) belongs not to an instance of a