Why can't I do this: dynamic x = new ExpandoObject { Foo = 12, Bar = “twelve” }

做~自己de王妃 提交于 2019-11-28 06:40:47

Am I doing something wrong, or is the following code really not possible?

It's really not possible. The thing on the left of the assignment operator has to be a property or field known at compile time, and obviously that is not the case for expando objects.

Why would the C# team opt to disallow the same initialization syntax as for regular objects, anonymous objects, and enumerables/lists?

The way you phrase the question indicates the logical error. Features are not implemented by default and then we run around disallowing almost all of them because we think they're a bad idea! Features are unimplemented by default, and have to be implemented in order to work.

The first step in implementing any feature is that someone has to think of it in the first place. To my knowledge, we never did. In particular, it would have been quite difficult for the person designing object initializers in 2006 to know that in 2010 we were going to add "dynamic" to the language, and design the feature accordingly. Features are always designed by designers who move forwards in time, not backwards in time. We only remember the past, not the future.

Anyway, it's a nice idea so thanks for sharing it. Now that someone has thought of it, we can then work on the next steps, like deciding if it is the best idea upon which we can spend our limited budget, designing it, writing the specification, implementing it, testing it, documenting it and shipping it to customers.

I wouldn't expect any of that to happen any time soon; we're a bit busy with this whole async-and-WinRT business that we announced at Build last week.

There's a better mouse trap than ExpandoObject. The dynamic keyword handles anonymous types with aplomb:

class Program {      
    static void Main(string[] args) {
        dynamic x = new { Foo = 12, Bar = "twelve" };
        Display(x);
    }
    static void Display(dynamic x) {
        Console.WriteLine(x.Foo);
        Console.WriteLine(x.Bar);
    }
}

One unfortunate problem is that the C# compiler generates the anonymous type giving the members only internal accessibility. Which means that you'll get a runtime error when you try to access the members in another assembly. Bummer.

Consider a tuple, much improved in C# v7.

Dynamitey (open source PCL and found in nuget) has a syntax for initializing expandos that can be inline.

 //using Dynamitey.DynamicObjects
 var x = Build<ExpandoObject>.NewObject(Foo:12, Bar:"twelve");

One workaround that worked for me is this ToExpando() extension method by Yan Cui (source):

public static class DictionaryExtensionMethods
{
    /// <summary>
    /// Extension method that turns a dictionary of string and object to an ExpandoObject
    /// </summary>
    public static ExpandoObject ToExpando(this IDictionary<string, object> dictionary)
    {
        var expando = new ExpandoObject();
        var expandoDic = (IDictionary<string, object>) expando;

        // go through the items in the dictionary and copy over the key value pairs)
        foreach (var kvp in dictionary)
        {
            // if the value can also be turned into an ExpandoObject, then do it!
            if (kvp.Value is IDictionary<string, object>)
            {
                var expandoValue = ((IDictionary<string, object>) kvp.Value).ToExpando();
                expandoDic.Add(kvp.Key, expandoValue);
            }
            else if (kvp.Value is ICollection)
            {
                // iterate through the collection and convert any strin-object dictionaries
                // along the way into expando objects
                var itemList = new List<object>();
                foreach (var item in (ICollection) kvp.Value)
                {
                    if (item is IDictionary<string, object>)
                    {
                        var expandoItem = ((IDictionary<string, object>) item).ToExpando();
                        itemList.Add(expandoItem);
                    }
                    else
                    {
                        itemList.Add(item);
                    }
                }

                expandoDic.Add(kvp.Key, itemList);
            }
            else
            {
                expandoDic.Add(kvp);
            }
        }

        return expando;
    }
}

Example usage:

public const string XEntry = "ifXEntry";
public static readonly dynamic XEntryItems = new Dictionary<string, object>
{
    { "Name",                     XEntry + ".1" },
    { "InMulticastPkts",          XEntry + ".2" },
    { "InBroadcastPkts",          XEntry + ".3" },
    { "OutMulticastPkts",         XEntry + ".4" },
    { "OutBroadcastPkts",         XEntry + ".5" },
    { "HCInOctets",               XEntry + ".6" },
    { "HCInUcastPkts",            XEntry + ".7" },
    { "HCInMulticastPkts",        XEntry + ".8" },
    { "HCInBroadcastPkts",        XEntry + ".9" },
    { "HCOutOctets",              XEntry + ".10" },
    { "HCOutUcastPkts",           XEntry + ".11" },
    { "HCOutMulticastPkts",       XEntry + ".12" },
    { "HCOutBroadcastPkts",       XEntry + ".13" },
    { "LinkUpDownTrapEnable",     XEntry + ".14" },
    { "HighSpeed",                XEntry + ".15" },
    { "PromiscuousMode",          XEntry + ".16" },
    { "ConnectorPresent",         XEntry + ".17" },
    { "Alias",                    XEntry + ".18" },
    { "CounterDiscontinuityTime", XEntry + ".19" },
}.ToExpando();

Then can use properties like XEntryItems.Name.

PS: Please vote here to support object initializers on ExpandoObjects.

That kind of initializer syntax is possible because there are already property with a get and setter. With the expando object there aren't those properties yet from what I can tell.

Much simpler way to do this:

Func<Dictionary<string, object>, ExpandoObject> parse = dict =>
{
    var expando = new ExpandoObject();
    foreach (KeyValuePair<string, object> entry in dict)
    {
        expando.Add(entry.Key, entry.Value);
    }
    return expando;
};

Then just call the parse function and pass in a dictionary as a parameter. This of course can be implemented into a method instead of a function.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!