如何将参数传递给 static class 构造函数?
How to pass parameter to static class constructor?
我有一个带有静态构造函数的静态 class。我需要以某种方式将参数传递给此静态 class 但我不确定最好的方法是什么。
你会推荐什么?
public static class MyClass {
static MyClass() {
DoStuff("HardCodedParameter")
}
}
不要使用静态构造函数,而是使用静态初始化方法:
public class A
{
private static string ParamA { get; set; }
public static void Init(string paramA)
{
ParamA = paramA;
}
}
在 C# 中,静态构造函数是无参数的,并且很少有方法可以克服此限制。一个是我上面给你的建议。
根据 MSDN,A static constructor is called automatically to initialize the class before the first instance is created。因此您不能发送任何参数。
CLR 必须调用静态构造函数,它如何知道要传递哪些参数?
所以不要使用静态构造函数。
这是满足您要求的解决方法。
public class StaticClass
{
private int bar;
private static StaticClass _foo;
private StaticClass() {}
static StaticClass Create(int initialBar)
{
_foo = new StaticClass();
_foo.bar = initialBar;
return _foo;
}
}
Static constructors have the following properties:
- A static constructor does not take access modifiers or have parameters. A static constructor is called automatically to
initialize the class before the first instance is created or any
static members are referenced.
- A static constructor cannot be called directly.
- The user has no control on when the static constructor is executed in the program.
- A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.
- Static constructors are also useful when creating wrapper classes for unmanaged code, when the constructor can call the LoadLibrary
method.
- If a static constructor throws an exception, the runtime will not invoke it a second time, and the type will remain uninitialized for
the lifetime of the application domain in which your program is
running.
如果 "HardCodedParameter" 你的意思是硬编码,你可以使用常量。
public static class YoursClass
{
public const string AnotherHardCodedParam = "Foo";
}
public static class MyClass
{
private const string HardCodedParam = "FooBar";
static MyClass()
{
DoStuff(MyClass.HardCodedParam);
DoStuff(YoursClass.AnotherHardCodedParam);
}
}
此外,您可以使用静态只读属性。
非静态 class 上的构造函数有利于确保在实际使用之前正确初始化它们。
因为静态 classes 没有这个好处,你必须确保自己。
使用名称明显的静态构造函数,然后在静态过程的相关部分检查以确保已执行初始化。
下面的示例假设您想要使用 Form 对象“初始化”静态 class。
public static class MyClass
{
private static Form FormMain { get; set; }
public static void Init(Form initForm)
{
FormMain = initForm;
}
private static bool InitCheck()
{
return FormMain != null ? true: false;
}
public static void DoStuff()
{
if (InitCheck())
{
// Do your things
}
else
{
throw new Exception("Object reference not set to an instance of an object");
}
}
}
我有一个带有静态构造函数的静态 class。我需要以某种方式将参数传递给此静态 class 但我不确定最好的方法是什么。 你会推荐什么?
public static class MyClass {
static MyClass() {
DoStuff("HardCodedParameter")
}
}
不要使用静态构造函数,而是使用静态初始化方法:
public class A
{
private static string ParamA { get; set; }
public static void Init(string paramA)
{
ParamA = paramA;
}
}
在 C# 中,静态构造函数是无参数的,并且很少有方法可以克服此限制。一个是我上面给你的建议。
根据 MSDN,A static constructor is called automatically to initialize the class before the first instance is created。因此您不能发送任何参数。
CLR 必须调用静态构造函数,它如何知道要传递哪些参数?
所以不要使用静态构造函数。
这是满足您要求的解决方法。
public class StaticClass
{
private int bar;
private static StaticClass _foo;
private StaticClass() {}
static StaticClass Create(int initialBar)
{
_foo = new StaticClass();
_foo.bar = initialBar;
return _foo;
}
}
Static constructors have the following properties:
- A static constructor does not take access modifiers or have parameters. A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.
- A static constructor cannot be called directly.
- The user has no control on when the static constructor is executed in the program.
- A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.
- Static constructors are also useful when creating wrapper classes for unmanaged code, when the constructor can call the LoadLibrary method.
- If a static constructor throws an exception, the runtime will not invoke it a second time, and the type will remain uninitialized for the lifetime of the application domain in which your program is running.
如果 "HardCodedParameter" 你的意思是硬编码,你可以使用常量。
public static class YoursClass
{
public const string AnotherHardCodedParam = "Foo";
}
public static class MyClass
{
private const string HardCodedParam = "FooBar";
static MyClass()
{
DoStuff(MyClass.HardCodedParam);
DoStuff(YoursClass.AnotherHardCodedParam);
}
}
此外,您可以使用静态只读属性。
非静态 class 上的构造函数有利于确保在实际使用之前正确初始化它们。
因为静态 classes 没有这个好处,你必须确保自己。
使用名称明显的静态构造函数,然后在静态过程的相关部分检查以确保已执行初始化。 下面的示例假设您想要使用 Form 对象“初始化”静态 class。
public static class MyClass
{
private static Form FormMain { get; set; }
public static void Init(Form initForm)
{
FormMain = initForm;
}
private static bool InitCheck()
{
return FormMain != null ? true: false;
}
public static void DoStuff()
{
if (InitCheck())
{
// Do your things
}
else
{
throw new Exception("Object reference not set to an instance of an object");
}
}
}