类级匿名类型?

mpe*_*pen 5 c#

是否可以创建类级别的匿名类型?喜欢,

class MyClass {
    readonly var _myAnon = new { Prop = "Hello" };
}
Run Code Online (Sandbox Code Playgroud)

我正在寻找一种简单的方法来创建类似字典的常量结构。


想要创建这样的东西,但具有更多的类型安全性:

    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" } }
        }
    };
Run Code Online (Sandbox Code Playgroud)

Tim*_*mwi 4

您始终可以声明自己的类型。它不需要\xe2\x80\x99t 是匿名的:

\n\n
class MyClass\n{\n    readonly MarkProperties _mark = new MarkProperties();\n    public class MarkProperties\n    {\n        public string Name = "Mark";\n        public int Age = 22;\n        public DateTime Birthdate = new DateTime(...);\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

因此,您在问题的编辑版本中给出的代码将类似于:

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