简体   繁体   English

未调用 Static 构造函数?

[英]Static constructor not called?

[Edit] I forgot to add the static Keyword and didn't relaize this because i called the class Statics [编辑] 我忘了添加 static 关键字并且没有重新解释这个,因为我调用了 class 静态

I am creating a Blazor client-side application.我正在创建一个 Blazor 客户端应用程序。

I am trying to create a static method that i want to return a Random name from a file.我正在尝试创建一个 static 方法,我想从文件中返回一个随机名称。

public class Statics
{
    private static List<string> _names;

    public Statics()
    {
        var assembly = Assembly.GetExecutingAssembly();
        var resourceName = "[AssemblyName].Stuff.first-names.json";

        using (Stream stream = assembly.GetManifestResourceStream(resourceName))
        using (StreamReader reader = new StreamReader(stream))
        {
            string result = reader.ReadToEnd();
            _names = JsonSerializer.Deserialize<List<string>>(result);
        }
    }

    public static string GetRandomName()
    {
        var random = new Random();
        return _names[random.Next(_names.Count)];
    }
}

I am trying to load the file in a static constructor, to not load the file more than one time, and insted save the names in memory.我正在尝试在 static 构造函数中加载文件,以不多次加载文件,并将名称保存在 memory 中。

But when i call the GetRandomName method i get a nullrefrenceexception on the list但是当我调用 GetRandomName 方法时,我在列表中得到一个 nullrefrenceexception

It works fine if i copy the code to load the file into the method如果我复制代码以将文件加载到方法中,它工作正常

   public static string GetRandomName()
    {
        var random = new Random();

        var assembly = Assembly.GetExecutingAssembly();
        var resourceName = "[AssemblyName].Stuff.first-names.json";

        using (Stream stream = assembly.GetManifestResourceStream(resourceName))
        using (StreamReader reader = new StreamReader(stream))
        {
            string result = reader.ReadToEnd();
            _names = JsonSerializer.Deserialize<List<string>>(result);
        }

        return _names[random.Next(_names.Count)];
    }

I have also tried just to populate the list with any string in the constructor (this didn't work either)我也尝试用构造函数中的任何字符串填充列表(这也不起作用)

 public class Statics
{
    private static List<string> _names;

    public Statics()
    {
        _names = new List<string>() { "hello", "World", "Something" };
    }

    public static string GetRandomName()
    {
        var random = new Random();

        return _names[random.Next(_names.Count)];
    }
}

I understand the concept of a static constructor in c# as我将 c# 中的 static 构造函数的概念理解为

When you reference a field or property in a static class with a static constructor.当您使用 static 构造函数引用 static class 中的字段或属性时。 you are insured that the constructor will run before you access the field or property (but you cannot control precisely when).您确信构造函数将在您访问字段或属性之前运行(但您无法精确控制何时)。

Is the problem in my code?我的代码有问题吗? my understanding?我的理解? or the blazer mono runtime?还是西装外套 mono 运行时?

Your class doesn't have a static constructor.您的 class 没有 static 构造函数。 public Statics() is an instance constructor. public Statics() 是一个实例构造函数。 To be a static constructor, you'd need to use the static keyword (and remove the public modifier).要成为 static 构造函数,您需要使用 static 关键字(并删除 public 修饰符)。

So change this:所以改变这个:

public Statics()

to

static Statics()

... and I suspect everything will work fine, although if your class is really only meant to include static members, you might want to declare it as a static class too. ... and I suspect everything will work fine, although if your class is really only meant to include static members, you might want to declare it as a static class too.

I'd also at least be wary of doing too much work in a static constructor.我至少也要警惕在 static 构造函数中做太多工作。 Problems in type initializers (including static constructors) can be very hard to resolve - and currently, you've got synchronous IO in a location that's quite hard for anything using your code to change in terms of the timing.类型初始值设定项(包括 static 构造函数)中的问题可能很难解决 - 目前,同步 IO 的位置对于任何使用您的代码来更改时序都非常困难。

Maybe it's okay - it's just something to be aware of.也许没关系 - 这只是需要注意的事情。

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

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