Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ C # dynamic class notes --- (Dynamic) Applications     - MongoDB Installation under CentOS 6.6 (Database)

- 5 interesting Linux command line tips (Linux)

- Ambari and Hadoop configuration, management and monitoring of the project Getting (Server)

- Linux source code analysis tool (Linux)

- MySQL stored procedures and triggers (Database)

- High-performance open-source http accelerator Varnish introduce (Server)

- Local port forwarding using iptables under linux (Server)

- Android Qemu GPS module (Programming)

- RedHat Redis Linux installation (Database)

- Android project and coding specifications (Programming)

- Boost notes --Asio - (1) a simple small example of synchronous communication (Programming)

- Storm how to ensure that at least once semantics (Programming)

- Use Spring cache and ehcache (Programming)

- The YUM package management under Linux (Linux)

- Diagnose and resolve the SSH connection slow (Linux)

- configuration ssh without password under Linux (Linux)

- MySQL EXPLAIN SQL output description (Database)

- Learning C ++ Standard Template Library and data structures (Programming)

- Build ASP.NET 5 development environment in Ubuntu (Server)

- Linux compression and decompression command (Linux)

 
         
  C # dynamic class notes --- (Dynamic) Applications
     
  Add Date : 2017-08-31      
         
         
         
  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.
     
         
         
         
  More:      
 
- Linux user opens a number of adjustment processes (Linux)
- Docker improve safety (Server)
- Java code JIT compiler-friendly Mody (Programming)
- Linux /var/spool/ insufficient clientmqueue space solutions (Linux)
- count (*) function in MySQL optimization of InnoDB storage engine (Database)
- Ubuntu the ARP (arptables) (Linux)
- The most concise Systemd tutorial, just ten minutes (Linux)
- Why do I prefer Git (Linux)
- APT-mirror using a four-step configuration Ubuntu local depot (Linux)
- Make Linux more secure server tips (Linux)
- Oracle 12CIN-memory in table spaces (Database)
- Java objects are taking up much space (Programming)
- A step by step teach have to install multi-node cluster configuration Hadoop (Server)
- Zabbix installation under Linux (Server)
- S5PV210 development board for embedded development environment to build under Ubuntu (Linux)
- C ++ class implementation date operator overloading (Programming)
- Getting Started Linux Shell Scripting (Programming)
- Oracle Database Delete Delete million or more common method of heap table data (Database)
- How to deploy Icinga client (Server)
- MySQL 5.5 on master-slave copy filter (Database)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.