私は大きな辞書を使用しています。一度だけ作成して情報を保存し、静的関数を使用して情報を抽出します。次のようになります。
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
は常にnullになります。これが当てはまらないことを確認するには、何をする必要がありますか?これを行うためのより良い方法に関する提案もいただければ幸いです。
私は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."}
アップデートII:繰り返されるキーはどこにありますか?