How can I use collection initializer syntax with ExpandoObject?

后端 未结 6 995
温柔的废话
温柔的废话 2021-02-07 08:24

I\'ve noticed that the new ExpandoObject implements IDictionary which has the requisite IEnumerable

6条回答
  •  花落未央
    2021-02-07 09:03

    The opensource framework Dynamitey has an alternative syntax for building ExpandoObject instances inline.

        dynamic obj = Builder.New(
            foo:"hello",
            bar: 42 ,
            baz: new object()
        );
    
        int value = obj.bar;
    

    It also has a dictionary based dynamic prototype object Dynamitey.DynamicObjects.Dictionary such that

        dynamic obj = new Dynamitey.DynamicObjects.Dictionary()
        {
            { "foo", "hello" },
            { "bar", 42 },
            { "baz", new object() }
        };
    
        int value = obj.bar;
    

    works too.

提交回复
热议问题