If it applies the same from my knowledge in C ++ that day ...
Generics are generated at compile time , and they are converted to many separate and different classes. For each of these classes, the static constructor works before any code references the static class. The CLR knows which types are referenced because each of the classes is unique.
EventSystemBase <string, byte> has absolutely no relationship with EventSystemBase <string, int>. It is as if you wrote two completely different classes in the source code.
class Program { public static void Main() { var myInt = new MyGeneric<int>(); myInt.InstanceMethod(); MyGeneric<int>.StaticMethod(); MyGeneric<long>.StaticMethod(); var myLong = new MyGeneric<long>(); myLong.InstanceMethod(); Console.ReadLine(); } } public class MyGeneric<T> { static MyGeneric() { Console.WriteLine("Static constructor: {0}", typeof(T).Name); } public static void StaticMethod() { Console.WriteLine("Static method: {0}", typeof(T).Name); } public void InstanceMethod() { Console.WriteLine("Instance method: {0}", typeof(T).Name); } }
source share