Is it possibly to create a class-level anonymous type? Like,
class MyClass {
readonly var _myAnon = new { Prop = "Hello" };
}
I’m looking for an easy way to create a dictionary-like structure of constants.
Would like to create something like this, but with a bit more type-safety:
readonly Dictionary<string, dynamic> _selectors = new Dictionary<string, dynamic>
{
{ "order", new string[] {"ID","NAME","TAG"} },
{ "match", new Dictionary<string, Regex> {
{ "ID", new Regex(@"#((?:[\w\u00c0-\uFFFF-]|\\.)+)") },
{ "CLASS", new Regex(@"\.((?:[\w\u00c0-\uFFFF-]|\\.)+)") },
{ "NAME", new Regex(@"\[name=['""]*((?:[\w\u00c0-\uFFFF-]|\\.)+)['""]*\]") },
{ "ATTR", new Regex(@"\[\s*((?:[\w\u00c0-\uFFFF-]|\\.)+)\s*(?:(\S?=)\s*(['""]*)(.*?)\3|)\s*\]") },
{ "TAG", new Regex(@"^((?:[\w\u00c0-\uFFFF\*-]|\\.)+)") },
{ "CHILD", new Regex(@":(only|nth|last|first)-child(?:\((even|odd|[\dn+-]*)\))?") },
{ "POS", new Regex(@":(nth|eq|gt|lt|first|last|even|odd)(?:\((\d*)\))?(?=[^-]|$)") },
{ "PSEUDO", new Regex(@":((?:[\w\u00c0-\uFFFF-]|\\.)+)(?:\((['""]?)((?:\([^\)]+\)|[^\(\)]*)+)\2\))?") } }
},
{ "leftMatch", new object() },
{ "attrMap", new Dictionary<string, string> {
{ "class", "className" },
{ "for", "htmlFor" } }
}
};
You can always declare your own type. It doesn’t need to be anonymous:
So the code that you gave in your edited version of your question would become something like: