C# 静态构造函数多次调用 使用系统; 命名空间控制台应用程序15 { 使用System.Collections.Generic; 使用系统线程; 公共静态类程序 { 静态void Main(字符串[]参数) { var test1=新测试(); var t=新螺纹(测试仪); t、 Start(); var test2=新测试(); var test3=新测试(); var test4=新测试(); var test5=新测试(); test1.Do(); test2.Do(); test3.Do(); test4.Do(); test5.Do(); } 专用静态空隙测试仪() { var test5=新测试(); test5.Do(); } } 公共类测试,其中T:IEnumerable { 私人静态的东西; 静态测试() { Console.WriteLine(“IM静态创建”); 某物=新的某物(); Console.WriteLine(something.ToString()); } 公开考试() { Console.WriteLine(“IM已创建”); } 公营部门 { Console.WriteLine(“做点什么!”); } } 公开课 { 公开某事 { Console.WriteLine(“创建某物”); } } }

C# 静态构造函数多次调用 使用系统; 命名空间控制台应用程序15 { 使用System.Collections.Generic; 使用系统线程; 公共静态类程序 { 静态void Main(字符串[]参数) { var test1=新测试(); var t=新螺纹(测试仪); t、 Start(); var test2=新测试(); var test3=新测试(); var test4=新测试(); var test5=新测试(); test1.Do(); test2.Do(); test3.Do(); test4.Do(); test5.Do(); } 专用静态空隙测试仪() { var test5=新测试(); test5.Do(); } } 公共类测试,其中T:IEnumerable { 私人静态的东西; 静态测试() { Console.WriteLine(“IM静态创建”); 某物=新的某物(); Console.WriteLine(something.ToString()); } 公开考试() { Console.WriteLine(“IM已创建”); } 公营部门 { Console.WriteLine(“做点什么!”); } } 公开课 { 公开某事 { Console.WriteLine(“创建某物”); } } },c#,C#,当我运行上面的代码时,我已经解释了在静态测试()中的静态construcor应该被调用一次,但是当我运行代码时,静态construcor被调用两次 当我删除this行时其中T:IEnumerable一切正常(static construcor调用过一次) 在C#中,泛型类型的特定参数化实际上是唯一的类型。这是将泛型类型信息作为运行时的一部分保留的优点,而不像Java这样的语言在编译过程中删除泛型类型信息 泛型类型中的静态构造函数有许多用例,例如: 泛型类型中的静态构造函数依赖于为每个关闭的泛

当我运行上面的代码时,我已经解释了在
静态测试()
中的静态construcor应该被调用一次,但是当我运行代码时,静态construcor被调用两次

当我删除this行时
其中T:IEnumerable
一切正常(static construcor调用过一次)

在C#中,泛型类型的特定参数化实际上是唯一的类型。这是将泛型类型信息作为运行时的一部分保留的优点,而不像Java这样的语言在编译过程中删除泛型类型信息

泛型类型中的静态构造函数有许多用例,例如:


泛型类型中的静态构造函数依赖于为每个关闭的泛型类型运行一次的另一种情况是类,如
EqualityComparer
,其中静态构造函数可用于为每个类型初始化一次
Default
属性,泛型类型为您指定的每个不同的
T
创建一个新类型

基本上,每个封闭泛型本身就是一个类型,而不管是从泛型模板定义的


这是静态成员的常见问题。

静态是每种类型,泛型类型是您指定的每种不同
T
的类型。基本上,每个封闭泛型本身就是一个类型,而不管是从
Test
Test
!=<代码>测试@AdamHouldsworth你应该将其作为答案发布。请将其作为答案发布!完成,但我的手机键盘没有用于格式化在线代码的字符…我需要在静态构造函数中进行一些工作(此工作将在AppDomain中调用一次),但正如您在本例中所述,构造函数可能被称为X时间。我应该使用单例模式还是你有其他想法?@BassamAlugili单例模式无论如何都会使用静态模式(或者很可能会)。我可能只是将其转移到一个静态类中,该类的唯一目的是包含这个静态成员,然后转移到其他方面。
using System;

namespace ConsoleApplication15
{
  using System.Collections.Generic;
  using System.Threading;

  public static class Program
  {
    static void Main(string[] args)
    {
      var test1 = new Test<List<int>>();
      var t = new Thread(Tester);
      t.Start();

      var test2 = new Test<List<int>>();
      var test3 = new Test<List<int>>();
      var test4 = new Test<List<int>>();
      var test5 = new Test<List<int>>();


      test1.Do();
      test2.Do();
      test3.Do();
      test4.Do();
      test5.Do();
    }

    private static void Tester()
    {
      var test5 = new Test<IList<int>>();
      test5.Do();
    }
  }

  public class Test<T> where T : IEnumerable<int>
  {

    private static Something something;

    static Test()
    {
      Console.WriteLine("IM  static created ");

      something = new Something();
      Console.WriteLine(something.ToString());
    }

    public Test()
    {
      Console.WriteLine("IM  created ");
    }

    public void Do()
    {
      Console.WriteLine("Do something! ");
    }
  }

  public class Something
  {
    public Something()
    {
      Console.WriteLine("Create something");
    }
  }
}