|
Background: Coding in sometimes encounter some data need to be resolved, but the number and names of field data is not unified, we can not define the corresponding entity class. Then we will think through the dynamic C # class to implement dynamic, if we pay attention to the words of some of the ORM framework which looks like there used to implement dynamic part of the function.
.Dynamic A basic application
1.1 by .PropertyName add attributes, and JavaScript object about the same. But for us to parse the data, we do not know in advance may attribute names, this method has little significance.
dynamic myObj = new ExpandoObject ();
myObj.Name = "Frank";
Console.WriteLine (myObj.Name);
Two .Dynamic custom property name.
2.1: Inheritance DynamicObject, which provides a variety of ways, after rewrite can add attributes.
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 by adding attributes and assignment dictionary
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
}
Three .Dynamic parsing XML.
3.1 Definitions xml file:
< ? Xml version = "1.0" encoding = "utf-8"?>
< Person>
< Name> Frank < / Name>
< Age> 23 < / Age>
< Address> TianFu SoftWarePark < / Address>
< / Person>
3.2 Inheritance 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 Results Output:
public static void Main (string [] args)
{
XElement root = XElement.Load (@ "Test.xml");
dynamic personList = new MyExtensXMLObj (root);
Console.WriteLine (personList.Name); // Frank
}
IV. Inheritance rules.
1. subclass which contains a private variable, used to store data for the time being this is called Data.;
2.TryGetMember (GetMemberBinder binder, out object result) way to achieve access to the data. Binder.Name is the name of the property needs to be acquired, result is the value of property acquired by binder.Name get to the corresponding attribute value in the Data, out to the outside. (noticed it result is out parameters)
3.TrySetMember (SetMemberBinder binder, object value) of existing property assignment. Set the above method, I judge whether there binder.Name data inside. If there can not be assigned. Return false, if the outside of the property does not exist then the copy will be given. |
|
|
|