Note that there are some explanatory texts on larger screens.

plurals
  1. POStatic constructor on a .NET interface is not run
    primarykey
    data
    text
    <p>You can define a static constructor on an interface in .NET in IL. However, if you do so, the static constructor is not run when you run a method on the interface:</p> <pre><code>.method public static void Main() { .entrypoint .locals init ( class IInterface cls1 ) // InterfaceClass static constructor is run newobj instance void InterfaceClass::.ctor() stloc.0 ldloc.0 // IInterface static constructor is not run!!!!! callvirt instance int32 IInterface::Method() call void [mscorlib]System.Console::WriteLine(int32) ret } .class public interface IInterface { .method private static specialname rtspecialname void .cctor() { ldstr "Interface static cctor" call void [mscorlib]System.Console::WriteLine(string) ret } .method public abstract virtual instance int32 Method() {} } .class public InterfaceClass implements IInterface { .method private static specialname rtspecialname void .cctor() { ldstr "Class static cctor" call void [mscorlib]System.Console::WriteLine(string) ret } .method public specialname rtspecialname instance void .ctor() { ldarg.0 call instance void [mscorlib]System.Object::.ctor() ret } .method public virtual instance int32 Method() { ldc.i4.s 42 ret } } </code></pre> <p>What's going on here? The CLR spec (Partition II, 10.5.3.1) says that when type initializers are executed is specified in Partition I, but I cannot find any reference in Partition I to type initializer execution.</p> <p>EDIT:</p> <p>I <em>can</em> get the interface static intitializer to run, but only by adding a static field to the interface, and accessing that field somewhere in the code, even if the field isn't actually assigned in the static constructor. So it seems that calling a method on an interface does not make the static constructor run, but accessing a field does. Why is this the case? And where is this mentioned in the spec?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload