背景: 在Coding中有时候会遇到一些需要解析的数据,可是数据的字段数量和名称未统一,我们没法定义实体类来对应。那么我们就会想到通过C#的dynamic动态类来实现,如果大家注意的话一些ORM框架里面貌似都有用到dynamic来实现一部分功能。
一.Dynamic的基本应用
1.1 通过.PropertyName来添加属性,和JavaScript的对象差不多.不过对于我们所要解析的数据,我们事先也许根本不知道属性名称,所以用这种方法意义不大.
dynamic myObj = new ExpandoObject();
myObj.Name = "Frank";
Console.WriteLine(myObj.Name);
二.Dynamic自定义属性名称.
2.1: 继承DynamicObject,里面提供了各种方法,重写后可以实现属性的添加.
public sealed class MyExtendsObject : DynamicObject
{
private readonly Dictionary<string, object> _properties;
public MyExtendsObject(Dictionary<string, object> properties)
{
_properties = properties;
}
public override IEnumerable<string> GetDynamicMemberNames()
{
return _properties.Keys;
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
if (_properties.ContainsKey(binder.Name))
{
result = _properties[binder.Name];
return true;
}
else
{
result = null;
return false;
}
}
public override bool TrySetMember(SetMemberBinder binder, object value)
{
if (_properties.ContainsKey(binder.Name))
{
_properties[binder.Name] = value;
return true;
}
else
{
return false;
}
}
}
2.2 通过字典来添加属性和赋值
public static void Main(string[] args)
{
dynamic myObj = new ExpandoObject();
Dictionary<string, object> dic = new Dictionary<string, object>()
{
{"Name","Frank"},
{"Age",23}
};
myObj = new MyExtendsObject(dic);
Console.WriteLine(myObj.Age); //23
}
三.Dynamic解析XML.
3.1 定义xml文件:
<?xml version="1.0" encoding="utf-8" ?>
<Person>
<Name>Frank</Name>
<Age>23</Age>
<Address>TianFu SoftWarePark</Address>
</Person>
3.2 继承DynamicObject
public sealed class MyExtensXMLObj : DynamicObject
{
private readonly XElement node;
public MyExtensXMLObj(XElement node)
{
this.node = node;
}
public override bool TrySetMember(SetMemberBinder binder, object value)
{
var elements = node.Elements().ToList();
var currentElement = elements.FirstOrDefault(x => x.Name == binder.Name);
if (currentElement != null)
{
currentElement.Value = value as string;
return true;
}
else
{
return false;
}
}