背景: 在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",}
}; myObj = new MyExtendsObject(dic);
Console.WriteLine(myObj.Age); //
}
三.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;
}
} public override bool TryGetMember(GetMemberBinder binder, out object result)
{
var elements = node.Elements().ToList();
var currentElement = elements.FirstOrDefault(x => x.Name == binder.Name);
if (currentElement != null)
{
result = currentElement.Value;
return true;
}
else
{
result = null;
return false;
}
}
}
3.3 结果输出:
public static void Main(string[] args)
{
XElement root = XElement.Load(@"Test.xml");
dynamic personList = new MyExtensXMLObj(root);
Console.WriteLine(personList.Name); // Frank
}
四. 继承规则.
1. 子类里面包含一个私有变量,用于存储数据. 这暂且叫做Data;
2.TryGetMember(GetMemberBinder binder, out object result) 方法实现对数据的获取. binder.Name就是需要获取的属性的名称,result 是获取的属性值. 通过binder.Name在Data中获取到对应的属性值,传出到外面.(注意到了吧result是out参数)
3.TrySetMember(SetMemberBinder binder, object value) 对存在的属性进行赋值. 上面的Set方法中,我都判断了binder.Name在data里面是否存在。如果不存在就无法赋值。返回false,如果外面对不存在的属性复制那么将会报错.