代码之家  ›  专栏  ›  技术社区  ›  Cheeso

C#类的构造函数顺序:参数化、默认和静态?

  •  1
  • Cheeso  · 技术社区  · 16 年前

    假设我有一个有3个构造函数的类,一个默认(无参数)构造函数、一个参数化构造函数和一个静态构造函数。这样地:

    public MyClass()  { ... }
    public MyClass(string arg) : this()  { ...  }
    static MyClass()  { ... }
    


    装配分解器

    static MyClass()
    {
        AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(Resolver);
    }
    

    static System.Reflection.Assembly Resolver(object sender, ResolveEventArgs args)
    {
        ....
    } 
    

    Assembly.GetExecutingAssembly().GetManifestResourceStream(name);
    

    你可能会说,

    Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'MyApp, Version=1.1.4.1, Culture=neutral, PublicKeyToken=edbe51ad942a3f5c' or one of its dependencies. The system cannot find the file specified.
    File name: 'MyApp, Version=1.1.4.1, Culture=neutral, PublicKeyToken=edbe51ad942a3f5c'
    
    WRN: Assembly binding logging is turned OFF.
    To enable assembly bind failure logging, set the registry value[HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
    Note: There is some performance penalty associated with assembly bind failure logging.
    To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].
    


    1 回复  |  直到 16 年前
        1
  •  5
  •   Reed Copsey    16 年前

    静态构造函数首先运行,然后是非参数化构造函数,最后是参数化构造函数。