我正在使用一个大字典,我只想创建一次来存储一些信息以及一个静态函数来从中提取信息,如下所示:
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."}
更新二:重复键在哪里?