[英]C# Static Dictionary Declare and Initialize in Abstract Class .NET 2.0
I have an abstract class and would like to add a static dictionary for error codes. 我有一个抽象类,想为错误代码添加一个静态字典。 I tried the following: 我尝试了以下方法:
public abstract class Base
{
...
protected static readonly Dictionary<int, string> errorDescriptions = new Dictionary<int, string>()
{
{ 1, "Description1"},
{ 2, "Description2"},
...
};
...
}
but then discovered that this was implemented in .NET 3.0; 但是后来发现这是在.NET 3.0中实现的; I am using 2.0. 我正在使用2.0。 I looked around and some others suggested I add the pairs in the constructor but this is an abstract class. 我环顾四周,还有一些建议我将这些对添加到构造函数中,但这是一个抽象类。
How can/should I populate the dictionary? 我如何/应该如何填充字典?
Thanks. 谢谢。
public abstract class Base
{
...
protected static readonly Dictionary<int, string> errorDescriptions;
// Type constructor called when Type is first accessed.
// This is called before any Static members are called or instances are constructed.
static Base ()
{
errorDescriptions = new Dictionary<int, string>();
errorDescriptions[1] = "Description1";
errorDescriptions[2] = "Description2";
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.