为什么我的静态字典没有初始化

本文关键字:初始化 字典 我的 静态 为什么 | 更新日期: 2023-09-27 17:49:30

我正在使用一个大字典,我想只创建一次来存储一些信息,以及一个静态函数来从中提取信息,如下所示:

public static class AceMimeInfo
{
    static Dictionary<string, Info> mimedictionary = new Dictionary<string, Info>
    {
        {".abap", new Info("abap", "ABAP")},
        {".asciidoc", new Info("asciidoc", "AsciiDoc")},
        {".c9search_results", new Info("c9search", "C9Search")},
        {".coffee", new Info("coffee", "CoffeeScript")},
        {".cf", new Info("coffee", "CoffeeScript")},
        {".xul", new Info("xml", "XML")},
        {".xbl", new Info("xml", "XML")},
        {".xq", new Info("xquery", "XQuery")},
        {".yaml", new Info("yaml", "YAML")}
    };
    public class Info
    {
        public string Mode;
        public string Name;
        public Info(string mode, string name)
        {
            Mode = mode;
            Name = name;
        }
    }
    public static string GetMode(string fileext)
    {
        string fe;
        fe = fileext.ToLower();
        if(!fe.StartsWith("."))
            fe = "." + fe;
        if (mimedictionary.ContainsKey(fe))
            return mimedictionary[fe].Mode;
        return "";
    }
}

然而,当我用调试器逐步通过GetMode时,字典mimedictionary始终为空。我要怎么做才能确保情况不是这样?如能提供更好的建议,我将不胜感激。

我像这样调用GetMode:

string mode = AceMimeInfo.GetMode("filename.cpp");

Thanks in advance

更新:

我尝试了上面的代码,它确实工作。上面的字典是我实际使用的一个较短的版本(因为它很长)。

这是我正在使用的实际类-它不起作用。当我缩短字典时,它确实有效。

public static class AceMimeInfo
{
    static Dictionary<string, Info> mimedictionary = new Dictionary<string, Info>
    {
        {".abap", new Info("abap", "ABAP")},
        {".asciidoc", new Info("asciidoc", "AsciiDoc")},
        {".c9search_results", new Info("c9search", "C9Search")},
        {".coffee", new Info("coffee", "CoffeeScript")},
        {".cf", new Info("coffee", "CoffeeScript")},
        {".cfm", new Info("coldfusion", "ColdFusion")},
        {".cs", new Info("csharp", "C#")},
        {".css", new Info("css", "CSS")},
        {".dart", new Info("dart", "Dart")},
        {".diff", new Info("diff", "Diff")},
        {".patch", new Info("diff", "Diff")},
        {".dot", new Info("dot", "Dot")},
        {".glsl", new Info("glsl", "Glsl")},
        {".frag", new Info("glsl", "Glsl")},
        {".vert", new Info("glsl", "Glsl")},
        {".go", new Info("golang", "Go")},
        {".groovy", new Info("groovy", "Groovy")},
        {".hx", new Info("haxe", "Haxe")},
        {".haml", new Info("haml", "HAML")},
        {".htm", new Info("html", "HTML")},
        {".html", new Info("html", "HTML")},
        {".xhtml", new Info("html", "HTML")},
        {".c", new Info("c_cpp", "C/C++")},
        {".cc", new Info("c_cpp", "C/C++")},
        {".cpp", new Info("c_cpp", "C/C++")},
        {".cxx", new Info("c_cpp", "C/C++")},
        {".h", new Info("c_cpp", "C/C++")},
        {".hh", new Info("c_cpp", "C/C++")},
        {".hpp", new Info("c_cpp", "C/C++")},
        {".clj", new Info("clojure", "Clojure")},
        {".jade", new Info("jade", "Jade")},
        {".java", new Info("java", "Java")},
        {".jsp", new Info("jsp", "JSP")},
        {".js", new Info("javascript", "JavaScript")},
        {".json", new Info("json", "JSON")},
        {".jsx", new Info("jsx", "JSX")},
        {".latex", new Info("latex", "LaTeX")},
        {".tex", new Info("latex", "LaTeX")},
        {".ltx", new Info("latex", "LaTeX")},
        {".bib", new Info("latex", "LaTeX")},
        {".less", new Info("less", "LESS")},
        {".lisp", new Info("lisp", "Lisp")},
        {".scm", new Info("lisp", "Lisp")},
        {".rkt", new Info("lisp", "Lisp")},
        {".liquid", new Info("liquid", "Liquid")},
        {".lua", new Info("lua", "Lua")},
        {".lp", new Info("luapage", "LuaPage")},
        {".lucene", new Info("lucene", "Lucene")},
        {".make", new Info("makefile", "Makefile")},
        {".md", new Info("markdown", "Markdown")},
        {".markdown", new Info("markdown", "Markdown")},
        {".m", new Info("objectivec", "Objective-C")},
        {".ml", new Info("ocaml", "OCaml")},
        {".mli", new Info("ocaml", "OCaml")},
        {".pl", new Info("perl", "Perl")},
        {".pm", new Info("perl", "Perl")},
        {".pgsql", new Info("pgsql", "pgSQL")},
        {".php", new Info("php", "PHP")},
        {".phtml", new Info("php", "PHP")},
        {".ps1", new Info("powershell", "Powershell")},
        {".py", new Info("python", "Python")},
        {".r", new Info("r", "R")},
        {".Rd", new Info("rdoc", "RDoc")},
        {".Rhtml", new Info("rhtml", "RHTML")},
        {".ru", new Info("ruby", "Ruby")},
        {".gemspec", new Info("ruby", "Ruby")},
        {".rake", new Info("ruby", "Ruby")},
        {".rb", new Info("ruby", "Ruby")},
        {".scad", new Info("scad", "OpenSCAD")},
        {".scala", new Info("scala", "Scala")},
        {".scss", new Info("scss", "SCSS")},
        {".sass", new Info("scss", "SCSS")},
        {".sh", new Info("sh", "SH")},
        {".bash", new Info("sh", "SH")},
        {".bat", new Info("sh", "SH")},
        {".sql", new Info("sql", "SQL")},
        {".styl", new Info("stylus", "Stylus")},
        {".stylus", new Info("stylus", "Stylus")},
        {".svg", new Info("svg", "SVG")},
        {".tcl", new Info("tcl", "Tcl")},
        {".tex", new Info("tex", "Tex")},
        {".txt", new Info("text", "Text")},
        {".textile", new Info("textile", "Textile")},
        {".typescript", new Info("typescript", "Typescript")},
        {".ts", new Info("typescript", "Typescript")},
        {".str", new Info("typescript", "Typescript")},
        {".xml", new Info("xml", "XML")},
        {".rdf", new Info("xml", "XML")},
        {".rss", new Info("xml", "XML")},
        {".wsdl", new Info("xml", "XML")},
        {".xslt", new Info("xml", "XML")},
        {".atom", new Info("xml", "XML")},
        {".mathml", new Info("xml", "XML")},
        {".mml", new Info("xml", "XML")},
        {".xul", new Info("xml", "XML")},
        {".xbl", new Info("xml", "XML")},
        {".xq", new Info("xquery", "XQuery")},
        {".yaml", new Info("yaml", "YAML")}
    };
    public class Info
    {
        public string Mode;
        public string Name;
        public Info(string mode, string name)
        {
            Mode = mode;
            Name = name;
        }
    }
    public static string GetMode(string fileext)
    {
        string fe;
        fe = fileext.ToLower();
        if(!fe.StartsWith("."))
            fe = "." + fe;
        if (mimedictionary.ContainsKey(fe))
            return "ace/mode/" + mimedictionary[fe].Mode;
        return "";
    }
}

调试器输出:

{"The type initializer for 'TestApp.AceMimeInfo' threw an exception."}

Update II:重复键在哪里?

为什么我的静态字典没有初始化

您将一个键多次添加到字典中,这会导致静态初始化器抛出异常。您可以通过检查所抛出异常的InnerException属性来确定这一点。

这很可能只是一个调试器工件。运行时可能会延迟静态字段的初始化,直到实际需要它们。仅在调试器中查看字段不会触发初始化,因此调试器可以观察到程序本身没有观察到的未初始化字段。

如果你的类有一个静态构造函数,它甚至会被迫延迟所有的静态初始化,直到你实例化类,访问一个静态字段,或者调用一个静态方法。

我很确定你的代码不会看到mimedictionary == null。你可以添加一个Debug.Assert(mimedictionary!=null),你会看到它不会被触发。


另一种可能导致问题的是在初始化式中构建一些循环调用。但是你的代码张贴没有这个属性。

我无法重现这个问题。在我的测试中,字典被填满并包含9个条目。问题是您的参数"filename.cpp"在字典中不是键。也不包含"。cpp"。如果你用"来调用,它就会起作用。

如果您想将其应用于文件名,请使用以下代码:

public static string GetMode(string fileOrExt)
{
    string ext = System.IO.Path.GetExtension(fileOrExt);
    if (ext == String.Empty) {
        ext = fileOrExt;
        if (!ext.StartsWith(".")) {
            ext = "." + ext;
        }
    }
    Info info;
    if (mimedictionary.TryGetValue(ext, out info)) {
        return info.Mode;
    }
    return "-";
}

我建议您创建一个静态构造函数,并在那里初始化所有静态类型,如下例所示:

public class AcmeMimeInfo
{
    private static List<string> list;
    static AcmeMimeInfo()
    {
        list = new List<string>();
    }
}