Passing static parameters to a class

冷暖自知 提交于 2019-12-19 02:47:31

问题


As far as I know you can can't pass parameters to a static constructor in C#. However I do have 2 parameters I need to pass and assign them to static fields before I create an instance of a class. How do I go about it?


回答1:


This may be a call for ... a Factory Method!

class Foo 
{ 
  private int bar; 
  private static Foo _foo;

  private Foo() {}

  static Foo Create(int initialBar) 
  { 
    _foo = new Foo();
    _foo.bar = initialBar; 
    return _foo;
  } 

  private int quux; 
  public void Fn1() {} 
} 

You may want to put a check that 'bar' is already initialized (or not) as appropriate.




回答2:


You can't pass parameters to a static constructor, but you can pass parameters to the class itself - via generic type parameters.

Slightly crazy this idea, however, I'll just throw it out there anyway.

Make the class generic (with a TypeParam that will provide a parameter type) and place generic constraints on it (details in code example), then derive a new parameter type, which contains virtuals that you can use to read what they want the parameter values to be.

//base parameter type - provides the 'anchor' for our generic constraint later, 
//as well as a nice, strong-typed access to our param values.
public class StaticParameterBase
{
  public abstract string ParameterString{ get; }
  public abstract MyComplexType ParameterComplex { get; }
}

//note the use of the new() generic constraint so we know we can confidently create
//an instance of the type.
public class MyType<TParameter> where TParameter:StaticParameterBase, new()
{
  //local copies of parameter values.  Could also simply cache an instance of
  //TParameter and wrap around that. 
  private static string ParameterString { get; set; }
  private static MyComplexType ParameterComplex { get; set; }

  static MyType()
  {
    var myParams = new TParameter();
    ParameterString = myParams.ParameterString;
    ParameterComplex = myParams.ParameterComplex;
  }
}

//e.g, a parameter type could be like this:
public class MyCustomParameterType : StaticParameterBase
{ 
  public override string ParameterString { get { return "Hello crazy world!"; } }
  public override MyComplexType { get {
      //or wherever this object would actually be obtained from.
      return new MyComplexType() { /*initializers etc */ };
    }
  }
}

//you can also now derive from MyType<>, specialising for your desired parameter type
//so you can hide the generic bit in the future (there will be limits to this one's
//usefulness - especially if new constructors are added to MyType<>, as they will 
//have to be mirrored on this type as well).
public class MyType2 : MyType<MyCustomParameterType> { }

//then you'd use the type like this:
public static void main()
{
  var instance = new MyType<MyCustomParameterType>();
  //or this:
  var instance2 = new MyType2();
}

I did consider a solution that employs custom type attributes applies to a type parameter, however this is easily a better way. However, you'll now be using your class always with a generic parameter type (unless you can use the deriving+specialisation trick) - possibly too clumsy for your liking.

I'd also prefer this over the other solutions presented here as it doesn't require creating any workarounds for the static initialisation - you can still use .Net's guarantee of single-time initialisation.

A word of warning - should you be reviewing your structure?

All that said - remember, though, since you can only parameterise the static once (or in this case, each uniquely parameterised static generic) - I would be asking myself why not just pull the code that is getting the parameters to give to the static, and place it in the static constructor in the first place? That way you don't actually have to resort to strange patterns like this!




回答3:


I assume you mean static members of a class? In that case, you can do this:

public class MyClass
{
    public static int MyInt = 12;
    public static MyOtherClass MyOther = new MyOtherClass();    
}

Those static members are guaranteed to be instantiated before any class is instantiated.

If you need complex logic, do it in a static constructor:

public class MyClass
{
    public static int MyInt;
    public static MyOtherClass MyOther;
    static MyClass()
    {
        MyInt = 12;
        MyOther = new MyOtherClass();
    }
}

Edit

Based on your edit, I'd say just assign the values to what they need to be before you instantiate the class, like so:

public class MyClass
{
    public static int MyInt;
    public static MyOtherClass MyOther;
}

// elsewhere in code, before you instantiate MyClass:
MyClass.MyInt = 12;
MyClass.MyOther = new MyOtherClass();
MyClass myClass = new MyClass();

That said, this method gives you no guarantee that MyInt and MyOther are set before MyClass is instantiated. It will work, but requires discipline before instantiating MyClass.

One alternative pattern you might follow looks like this:

public class MyClass
{
    private static int MyInt;
    private static MyOtherClass MyOther;
    private static bool IsStaticInitialized = false;

    public static InitializeStatic(int myInt, MyOtherClass other)
    {
        MyInt = myInt;
        MyOther = other;
        IsStaticInitialized = true;
    }

    public MyClass()
    {
        if(!IsStaticInitialized)
        {
            throw new InvalidOperationException("Static Not Initialized");
        }
        // other constructor logic here. 
    }
}

// elsewhere in your code:
MyClass.InitializeStatic(12, new MyOtherClass());
MyClass myClass = new MyClass();

// alternatiavely:
MyClass myClass = new MyClass(); // runtime exception. 


来源:https://stackoverflow.com/questions/2767017/passing-static-parameters-to-a-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!