遗憾的是,添加动态属性(其名称仅在运行时才知道)ExpandoObject
并不像应有的那么容易。所有转换为字典的内容都很丑陋。没关系,你总是可以编写一个DynamicObject
实现的自定义,Add
它可以帮助你使用整洁的对象初始化器,如语法。
一个粗略的例子:
public sealed class Expando : DynamicObject, IDictionary<string, object>
{
readonly Dictionary<string, object> _properties = new Dictionary<string, object>();
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
return _properties.TryGetValue(binder.Name, out result);
}
public override bool TrySetMember(SetMemberBinder binder, object value)
{
if (binder.Name == "Add")
{
var del = value as Delegate;
if (del != null && del.Method.ReturnType == typeof(void))
{
var parameters = del.Method.GetParameters();
if (parameters.Count() == 2 && parameters.First().ParameterType == typeof(string))
throw new RuntimeBinderException("Method signature cannot be 'void Add(string, ?)'");
}
}
_properties[binder.Name] = value;
return true;
}
object IDictionary<string, object>.this[string key]
{
get
{
return _properties[key];
}
set
{
_properties[key] = value;
}
}
int ICollection<KeyValuePair<string, object>>.Count
{
get { return _properties.Count; }
}
bool ICollection<KeyValuePair<string, object>>.IsReadOnly
{
get { return false; }
}
ICollection<string> IDictionary<string, object>.Keys
{
get { return _properties.Keys; }
}
ICollection<object> IDictionary<string, object>.Values
{
get { return _properties.Values; }
}
public void Add(string key, object value)
{
_properties.Add(key, value);
}
bool IDictionary<string, object>.ContainsKey(string key)
{
return _properties.ContainsKey(key);
}
bool IDictionary<string, object>.Remove(string key)
{
return _properties.Remove(key);
}
bool IDictionary<string, object>.TryGetValue(string key, out object value)
{
return _properties.TryGetValue(key, out value);
}
void ICollection<KeyValuePair<string, object>>.Add(KeyValuePair<string, object> item)
{
((ICollection<KeyValuePair<string, object>>)_properties).Add(item);
}
void ICollection<KeyValuePair<string, object>>.Clear()
{
_properties.Clear();
}
bool ICollection<KeyValuePair<string, object>>.Contains(KeyValuePair<string, object> item)
{
return ((ICollection<KeyValuePair<string, object>>)_properties).Contains(item);
}
void ICollection<KeyValuePair<string, object>>.CopyTo(KeyValuePair<string, object>[] array, int arrayIndex)
{
((ICollection<KeyValuePair<string, object>>)_properties).CopyTo(array, arrayIndex);
}
bool ICollection<KeyValuePair<string, object>>.Remove(KeyValuePair<string, object> item)
{
return ((ICollection<KeyValuePair<string, object>>)_properties).Remove(item);
}
IEnumerator<KeyValuePair<string, object>> IEnumerable<KeyValuePair<string, object>>.GetEnumerator()
{
return _properties.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable)_properties).GetEnumerator();
}
}
你可以随心所欲地打电话:
dynamic obj = new Expando()
{
{ "foo", "hello" },
{ "bar", 42 },
{ "baz", new object() }
};
int value = obj.bar;
这种方法的警告是,您不能将Add
具有与 Dictionary.Add 相同签名的“方法”添加到您的 expando 对象,因为它已经是Expando
该类的有效成员(这是集合初始化程序语法所必需的)。如果你这样做,代码会引发异常
obj.Add = 1; // runs
obj.Add = new Action<string, object>(....); // throws, same signature
obj.Add = new Action<string, int>(....); // throws, same signature for expando class
obj.Add = new Action<string, object, object>(....); // runs, different signature
obj.Add = new Func<string, object, int>(....); // runs, different signature
如果属性名称不需要真正动态,那么另一种选择是使用ToDynamic
扩展方法,以便您可以在线初始化。
public static dynamic ToDynamic(this object item)
{
var expando = new ExpandoObject() as IDictionary<string, object>;
foreach (var propertyInfo in item.GetType().GetProperties())
expando[propertyInfo.Name] = propertyInfo.GetValue(item, null);
return expando;
}
所以你可以打电话:
var obj = new { foo = "hello", bar = 42, baz = new object() }.ToDynamic();
int value = obj.bar;
有一百种方法可以为此设计 API,另一种方法(在 orad 的回答中提到)是:
dynamic obj = new Expando(new { foo = "hello", bar = 42, baz = new object() });
将是微不足道的实施。
旁注:如果您静态地知道属性名称并且您不想在初始化后进一步添加,那么总会有匿名类型。