background: in coding, sometimes there are some data that need to be parsed, but the number of fields and the names of the data are not uniform, we can't define the entity classes to correspond. Then we will think of the dynamic class of C # to achieve, if you are aware of some of the ORM framework seems to be useful to the dynamic to achieve a part of the function.
I. Basic applications of dynamic
1.1 Passed. PropertyName to add properties, similar to JavaScript objects. But for the data we are going to parse, we may not know the attribute name in advance, so it doesn't make much sense to use this method.
New"Frank"; Console.WriteLine (myobj.name);
two. Dynamic custom property name.
2.1: Inherited DynamicObject, which provides various methods, rewrite can implement the addition of attributes.
Public Sealed classMyextendsobject:dynamicobject {Private ReadOnlydictionary<string,Object>_properties; PublicMyextendsobject (dictionary<string,Object>properties) {_properties=properties; } Public Overrideienumerable<string>Getdynamicmembernames () {return_properties. Keys; } Public Override BOOLTrygetmember (GetMemberBinder binder, out Objectresult) { if(_properties. ContainsKey (binder. Name) {result=_properties[binder. Name]; return true; } Else{result=NULL; return false; } } Public Override BOOLTrysetmember (SetMemberBinder Binder,Objectvalue) { if(_properties. ContainsKey (binder. Name) {_properties[binder. Name]=value; return true; } Else { return false; } } }
2.2 Adding attributes and assignments through a dictionary
Public Static voidMain (string[] args) {Dynamic MYOBJ=NewExpandoObject (); Dictionary<string,Object> dic =Newdictionary<string,Object>() { {"Name","Frank"}, {" Age", at} }; MYOBJ=NewMyextendsobject (DIC); Console.WriteLine (Myobj.age); // at}
three. Dynamic parses the XML.
3.1 Define the 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.e Lements (). 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 ele ments = 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 Result output:
Public Static void Main (string[] args) { = xelement.load (@ "test.xml"); New myextensxmlobj (root); Frank }
four. Inheritance rules.
1. The subclass contains a private variable for storing data. This is called data for the moment;
The 2.TryGetMember (GetMemberBinder binder, out object result) method implements the acquisition of data. Binder. Names is the name of the property to get, and result is the property value to get. Through binder. Name gets the corresponding property value in data, out of the box. (Notice that result is an out parameter)
3.TrySetMember (SetMemberBinder Binder, Object value) assigns a value to the existing property. In the Set method above, I have judged the binder. Name exists in data. You cannot assign a value if it does not exist. Returns False if an outside copy of a property that does not exist will cause an error.
C # Notes---Dynamic class applications