English 中文(简体)
In C#, is there a way to write custom object initializers for new data-types?
原标题:

In C#, there s the "standard" initializer technique { Property1 = "a", Property2 = "b" }, and there are a couple of special variants for collections (list and dictionary). {value1, value2}, and { {"key1", value1 }, {"key2", value2} }.

I d like to have a recursive object initializer for a tree data type, but I don t know if there s any way to customize that mechanism. I d like something that looks like an s-expression. { item1 {item2 item3 item4 } {item5 item6} }

I m doing this via constructors, but I d like a terser syntax.

最佳回答

If you implement the ICollection IEnumerable interface and have a method called add. Incidentally, this is all included in the ICollection interface which is why I confused it.

Test test = new Test() {
    new Test2() {
        new Test3() {

        }
    },
    new Test() {
        new Test2() {
            { new Test(), new Test2() },
            { new Test(), new Test2() },
            { new Test(), new Test2() }
        }
    }
};

public class Test : IEnumerable
{
    public void Add(Test a){}
    public void Add(Test2 a){}
    public IEnumerator GetEnumerator(){}
}

public class Test2 : IEnumerable
{
    public void Add(Test a, Test2 b){}
    public void Add(Test3 a){}
    public IEnumerator GetEnumerator(){}
}

public class Test3 : IEnumerable
{
    public void Add(Test a) {}
    public void Add(Test2 a){}
    public IEnumerator GetEnumerator(){}
}
问题回答

Using a variadic local lambda n that simply calls your constructor, you could get it as short as:

n(item1, n(item2, item3, item4), n(item5, item6))

Update: something like

var n = (params Node[] nodes) => new Node(nodes);

If the nodes of your tree are easy to construct, i.e. can be initialized from their value, then you can make things terser than ChaosPandion s answer, by adding an extra method:

class Tree : IEnumerable
{
    public string Value { get; set; }

    public void Add(Tree t) { ... }

    // Add this method
    public void Add(string s)
    {
        Add(new Tree { Value = s });
    }

    public IEnumerator GetEnumerator() { ... }
}

So:

{ item1 { item2 item3 item4 } { item5 item6 } }

Becomes:

new Tree { "item1", new Tree { "item2", "item3", "item4" }, new Tree { "item5", "item6" } };




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签