0

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

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");

提前致谢

更新:

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

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

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."}

更新二:重复键在哪里?

4

4 回答 4

5

您不止一次向字典添加键,这导致静态初始化程序引发异常。您可以通过检查InnerException引发的异常的属性来确定这一点。

于 2012-12-26T17:56:48.490 回答
1

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

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

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


另一种可能导致麻烦的可能性是在初始化程序中构建一些循环调用。但是您发布的代码没有此属性。

于 2012-12-26T17:29:31.853 回答
0

我无法重现该问题。字典已填满,在我的测试中包含 9 个条目。问题是您的参数“filename.cpp”不是字典中的键。并且也不包含“.cpp”。例如,如果您使用“.cf”调用它,它会起作用。

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

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 "-";
}
于 2012-12-26T17:41:15.783 回答
-4

I would suggest you create a static constructor and initialize all your static types there like in the following example:

public class AcmeMimeInfo
{
    private static List<string> list;
    static AcmeMimeInfo()
    {
        list = new List<string>();
    }
}
于 2012-12-26T17:25:13.947 回答