C# 静态成员在构造函数中初始化后获取 null
C# static member gets null after init in Contructor
我有一个 class 保存静态 ImageSource 对象,这些对象以后经常被其他 classes:
public class ImagePrepare
{
public static readonly ImageSource m_imageGreen;
public static readonly ImageSource m_imageYellow;
public static readonly ImageSource m_imageRed;
public static readonly ImageSource m_imagePurple;
public static int iTest;
//static Constructor
static ImagePrepare()
{
iTest = 2;
Uri uriImage = new Uri("pack://application:,,,/" + Assembly.GetExecutingAssembly().GetName().Name + ";component/" + "Ressourcen/Button_Green.png", UriKind.Absolute);
ImageSource m_imageGreen = new BitmapImage(uriImage);
uriImage = new Uri("pack://application:,,,/" + Assembly.GetExecutingAssembly().GetName().Name + ";component/" + "Ressourcen/Button_Yellow.png", UriKind.Absolute);
ImageSource m_imageYellow = new BitmapImage(uriImage);
uriImage = new Uri("pack://application:,,,/" + Assembly.GetExecutingAssembly().GetName().Name + ";component/" + "Ressourcen/Button_Red.png", UriKind.Absolute);
ImageSource m_imageRed = new BitmapImage(uriImage);
uriImage = new Uri("pack://application:,,,/" + Assembly.GetExecutingAssembly().GetName().Name + ";component/" + "Ressourcen/Button_Purple.png", UriKind.Absolute);
ImageSource m_imagePurple = new BitmapImage(uriImage);
}
public static ImageSource GetImageSource()
{
return m_imageGreen;
}
}
现在,例如,当我从 MainWindow class 调用 ImagePrepare.GetImageSource() 时,首先调用静态 ctor 并且所有静态成员都按预期正确初始化。
然后调用 GetImageSource(),但在调试此函数时,成员 m_imageGreen 为空!
我在这里错过了什么?
成员 iTest 的行为与预期一致,并且仍然保持其值 2。
在您的静态构造函数中,您将静态成员重载为本地成员。
通过调用:
ImageSource m_imageGreen = new BitmapImage(uriImage);
您实际上创建了一个新的局部变量,而不是引用静态变量。
这样做:
m_imageGreen = new BitmapImage(uriImage);
我有一个 class 保存静态 ImageSource 对象,这些对象以后经常被其他 classes:
public class ImagePrepare
{
public static readonly ImageSource m_imageGreen;
public static readonly ImageSource m_imageYellow;
public static readonly ImageSource m_imageRed;
public static readonly ImageSource m_imagePurple;
public static int iTest;
//static Constructor
static ImagePrepare()
{
iTest = 2;
Uri uriImage = new Uri("pack://application:,,,/" + Assembly.GetExecutingAssembly().GetName().Name + ";component/" + "Ressourcen/Button_Green.png", UriKind.Absolute);
ImageSource m_imageGreen = new BitmapImage(uriImage);
uriImage = new Uri("pack://application:,,,/" + Assembly.GetExecutingAssembly().GetName().Name + ";component/" + "Ressourcen/Button_Yellow.png", UriKind.Absolute);
ImageSource m_imageYellow = new BitmapImage(uriImage);
uriImage = new Uri("pack://application:,,,/" + Assembly.GetExecutingAssembly().GetName().Name + ";component/" + "Ressourcen/Button_Red.png", UriKind.Absolute);
ImageSource m_imageRed = new BitmapImage(uriImage);
uriImage = new Uri("pack://application:,,,/" + Assembly.GetExecutingAssembly().GetName().Name + ";component/" + "Ressourcen/Button_Purple.png", UriKind.Absolute);
ImageSource m_imagePurple = new BitmapImage(uriImage);
}
public static ImageSource GetImageSource()
{
return m_imageGreen;
}
}
现在,例如,当我从 MainWindow class 调用 ImagePrepare.GetImageSource() 时,首先调用静态 ctor 并且所有静态成员都按预期正确初始化。
然后调用 GetImageSource(),但在调试此函数时,成员 m_imageGreen 为空!
我在这里错过了什么?
成员 iTest 的行为与预期一致,并且仍然保持其值 2。
在您的静态构造函数中,您将静态成员重载为本地成员。
通过调用:
ImageSource m_imageGreen = new BitmapImage(uriImage);
您实际上创建了一个新的局部变量,而不是引用静态变量。 这样做:
m_imageGreen = new BitmapImage(uriImage);