【问题标题】:Dynamically adding properties to an ExpandoObject向 ExpandoObject 动态添加属性
【发布时间】:2011-06-23 17:30:33
【问题描述】:

我想在运行时向 ExpandoObject 动态添加属性。因此,例如添加一个字符串属性调用 NewProp 我想写类似

var x = new ExpandoObject();
x.AddProperty("NewProp", System.String);

这很容易吗?

【问题讨论】:

标签: c# c#-4.0 dynamic expandoobject


【解决方案1】:
dynamic x = new ExpandoObject();
x.NewProp = string.Empty;

或者:

var x = new ExpandoObject() as IDictionary<string, Object>;
x.Add("NewProp", string.Empty);

【讨论】:

  • 我从来没有意识到 Expando 实现 IDictionary。我一直认为演员会将其复制到字典中。但是,您的帖子让我明白,如果您更改 Dictionary,您也会更改底层 ExpandoObject!非常感谢
  • 获取Error 53 Cannot convert type 'System.Dynamic.ExpandoObject' to 'System.Collections.Generic.IDictionary&lt;string,string&gt;' via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion
  • 这是IDictionary&lt;string, object&gt;,而不是IDictionary&lt;string, string&gt;。
  • 请务必注意,当您转换为 IDictionary 时,不要使用 dynamic 作为变量类型。
  • 如果你实现为 IDictionary,那么你不必强制转换它来使用来自父类或派生类的方法:IDictionary&lt;string, object&gt; myExpando = new ExpandoObject();
【解决方案2】:

Filip 在此解释 - http://www.filipekberg.se/2011/10/02/adding-properties-and-methods-to-an-expandoobject-dynamicly/

您也可以在运行时添加方法。

x.Add("Shout", new Action(() => { Console.WriteLine("Hellooo!!!"); }));
x.Shout();

【讨论】:

  • 您的代码完全错误,您跳过了最重要的部分,即转换为字典。
【解决方案3】:

这是一个示例帮助类,它转换一个对象并返回一个具有给定对象的所有公共属性的 Expando。

public static class dynamicHelper
    {
        public static ExpandoObject convertToExpando(object obj)
        {
            //Get Properties Using Reflections
            BindingFlags flags = BindingFlags.Public | BindingFlags.Instance;
            PropertyInfo[] properties = obj.GetType().GetProperties(flags);

            //Add Them to a new Expando
            ExpandoObject expando = new ExpandoObject();
            foreach (PropertyInfo property in properties)
            {
                AddProperty(expando, property.Name, property.GetValue(obj));
            }

            return expando;
        }

        public static void AddProperty(ExpandoObject expando, string propertyName, object propertyValue)
        {
            //Take use of the IDictionary implementation
            var expandoDict = expando as IDictionary<String, object>;
            if (expandoDict.ContainsKey(propertyName))
                expandoDict[propertyName] = propertyValue;
            else
                expandoDict.Add(propertyName, propertyValue);
        }
    }

用法:

//Create Dynamic Object
dynamic expandoObj= dynamicHelper.convertToExpando(myObject);
    
//Add Custom Properties
dynamicHelper.AddProperty(expandoObj, "dynamicKey", "Some Value");

【讨论】:

  • "var expandoDict = expando as IDictionary;"此行需要更改为“var expandoDict = expando as IDictionary;”
  • 为了让它更棒,使用这些作为扩展方法。
【解决方案4】:

我认为这会添加所需类型的新属性,而无需设置原始值,例如在类定义中定义属性时

var x = new ExpandoObject();
x.NewProp = default(string)

【讨论】:

  • 嘿莫特萨!纯代码答案可能会解决问题,但如果您解释它们如何解决问题,它们会更有用。社区需要理论和代码才能完全理解您的答案。
猜你喜欢
  • 1970-01-01
  • 2011-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-10
相关资源
最近更新 更多