mpen
mpen

Reputation: 283355

Class-level anonymous types?

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

Upvotes: 5

Views: 492

Answers (1)

Timwi
Timwi

Reputation: 66614

You can always declare your own type. It doesn’t need to be anonymous:

class MyClass
{
    readonly MarkProperties _mark = new MarkProperties();
    public class MarkProperties
    {
        public string Name = "Mark";
        public int Age = 22;
        public DateTime Birthdate = new DateTime(...);
    }
}

So the code that you gave in your edited version of your question would become something like:

readonly Selectors _selectors = new Selectors();
public class Selectors
{
    public string[] Order = { "ID", "NAME", "TAG" };
    public Match Match = new Match();
    public class Match
    {
        public Regex ID = new Regex(@"#((?:[\w\u00c0-\uFFFF-]|\\.)+)");
        public Regex CLASS = new Regex(@"\.((?:[\w\u00c0-\uFFFF-]|\\.)+)");
        public Regex NAME = new Regex(@"\[name=['""]*((?:[\w\u00c0-\uFFFF-]|\\.)+)['""]*\]");
        public Regex ATTR = new Regex(@"\[\s*((?:[\w\u00c0-\uFFFF-]|\\.)+)\s*(?:(\S?=)\s*(['""]*)(.*?)\3|)\s*\]");
        public Regex TAG = new Regex(@"^((?:[\w\u00c0-\uFFFF\*-]|\\.)+)");
        public Regex CHILD = new Regex(@":(only|nth|last|first)-child(?:\((even|odd|[\dn+-]*)\))?");
        public Regex POS = new Regex(@":(nth|eq|gt|lt|first|last|even|odd)(?:\((\d*)\))?(?=[^-]|$)");
        public Regex PSEUDO = new Regex(@":((?:[\w\u00c0-\uFFFF-]|\\.)+)(?:\((['""]?)((?:\([^\)]+\)|[^\(\)]*)+)\2\))?");
    }
    public object LeftMatch = new object();
    public AttrMap AttrMap = new AttrMap();
    public class AttrMap
    {
        public string Class = "className";
        public string For = "htmlFor";
    }
}

Upvotes: 4

Related Questions