c# - Why aren't these static constructors called in generic inherited types? -


here program not trigger static constructors though displays distinct owned static instances.

class program {     static void main(string[] args)     {         template.render();         template2.render();         console.writeline(referenceequals(template.factory, template2.factory));     } }  public class basetemplate<t> t : new() {     static basetemplate()     {         console.writeline("basetemplate() " + typeof(t).name);     }      public static object factory = new object();      public static void render()     {         console.writeline("render() " + typeof(t).name);     } }  public class template : basetemplate<template> {     static template()     {         console.writeline("static ctor()");     } }  public class template2 : basetemplate<template2> {     static template2()     {         console.writeline("static ctor() 2");     } } 

the result

basetemplate() template render() template basetemplate() template2 render() template2 false 

the goal here have custom instance of factory each sub-class, works fine, initialize in static constructor. can see factory instances distinct reference test, static constructors not called unless called system.runtime.compilerservices.runtimehelpers.runclassconstructor(typeof(t).typehandle);

then result following.

static ctor() basetemplate() template render() template static ctor() 2 basetemplate() template2 render() template2 false 

from the documentation:

a static constructor used initialize static data, or perform particular action needs performed once only. called automatically before first instance created or static members referenced.

so trigger static ctor must either

  1. create instance of template or template2. example doesn't.

  2. call static member of template or template2. in example, neither class has static members (other ctor) aren't calling them.

if modify code:

template.render(); template2.render(); var o = new template(); var p = new template2(); console.writeline(referenceequals(template.factory, template2.factory)); 

the output is:

basetemplate() template render() template basetemplate() template2 render() template2 static ctor() static ctor() 2 false 

with static methods there no inheritance (that requires vmt , therefore instance) chain static constructor calls this:

public class basetemplate<t> t : new() {     static basetemplate()     {         console.writeline("basetemplate() " + typeof(t).name);     }      public static object factory = new object();      public static void render()     {         console.writeline("render() basetemplate, " + typeof(t).name);     } }  public class template : basetemplate<template> {     static template()     {         console.writeline("static ctor()");     }      public static new void render()     {         console.writeline("render() template.");         basetemplate<template>.render();     } }  public class template2 : basetemplate<template2> {     static template2()     {         console.writeline("static ctor() 2");     }      public static new void render()     {         console.writeline("render() template2.");         basetemplate<template2>.render();     }  } 

which has output:

static ctor() render() template. basetemplate() template render() basetemplate, template static ctor() 2 render() template2. basetemplate() template2 render() basetemplate, template2 false 

Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -