简体   繁体   English

如何在初始化静态类之前初始化静态属性?

[英]How to initialize a static property before initializing a static class?

Below Class2 has a property that needs to be set before GetSomething is called, however because I access Class2 at the top of Class1 the property is always null when it gets to Something class. Class2下面有一个需要在GetSomething之前设置的属性,但是因为我访问Class1顶部的Class2 ,当它到达Something类时,该属性总是为null。 I can't seem to figure out how to change my code to set the property before it's used. 我似乎无法弄清楚如何更改我的代码以在使用之前设置属性。 Anyone? 任何人?

EDIT I want to pass the dependency from form1's constructor, not hardcode it further up the chain. 编辑我想从form1的构造函数传递依赖,而不是在链上进一步硬编码。

public partial class form1
{
    private static readonly ISomeConstructedClass someConstructedClass = Class1.SomeConstructedClass;
    public form1()
    {
        someConstructedClass.SomeDependency = new SomeDependency();
        someConstructedClass.Whatever();
    }
}
public static class Class1
{
    public static readonly ISomething something = (ISomething)Class2.GetSomething("something");
    public static ISomeConstructedClass SomeConstructedClass
    {
     get
     {
       return something.SomeConstructedClass;
     }
    }

} .... } } ....}

public class Class2
{   
    public static ISomeDependency SomeDependency
    {
       get;
       set;
    }
    public static GetSomething(string something)
    {
         switch(something)
         {
            case "something":
               return new Something( SomeDependency );
         }         
    }
}

  public class Something : ISomething
  {
      public ISomeDependency SomeDependency
      {
           get;
           set;
      }
      public Something(ISomeDependency someDependency)
      {
           SomeDependency = someDependency;
      }
  }

[Re]Edit: [回复]编辑:

I was confused about what you were trying to do before, you just need to create the dependency first. 我对你之前想要做的事感到困惑,你只需要先创建依赖关系。

public partial class form1
{
    private static /*readonly*/ ISomeConstructedClass someConstructedClass;
    public form1()
    {
        Class2.SomeDependency = new SomeDependency();
        someConstructedClass = Class1.SomeConstructedClass;
        someConstructedClass.Whatever();
    }
}

I would also move the creation of something into the property just to make sure it is not initialized too soon (before the form1 constructor is called). 我还会将一些东西的创建移动到属性中,以确保它不会太快初始化(在调用form1构造函数之前)。

public static class Class1
{
    public static ISomething something;
    public static ISomeConstructedClass SomeConstructedClass
    {
        get
        {
            if (something == null) {
                something = (ISomething)Class2.GetSomething("something");
            }
            return something.SomeConstructedClass;
        }
    }
}

You can use a static constructor . 您可以使用静态构造函数 This is called before any static (or instance for that matter) fields or methods are called/accessed. 在调用/访问任何静态(或该实例的实例)字段或方法之前调用此方法。

Something like: 就像是:

static Class2() {
    SomeDependency = SomeDependencyYouNeed;
}

Why are you using static methods? 你为什么使用静态方法? It looks like you're attempting a sort of Dependency Injection. 看起来你正在尝试一种依赖注入。 Either create an instance of Class2 and pass the dependency in the constructor (and don't use static methods), or pass the dependency as a parameter of the GetSomething() method. 创建Class2的实例并在构造函数中传递依赖项(并且不使用静态方法),或者将依赖项作为GetSomething()方法的参数传递。

public static GetSomething(string something, ISomeDependency dependency).

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM