Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ C # dynamic class notes --- (Dynamic) Applications     - Use GNU / Linux broadcasting of television programs (Linux)

- Nginx request processing (Server)

- Oracle 11g contraction table space error ORA-03297: file contains used data beyondrequested RESIZE value (Database)

- Permissions and attributes of files and directories under Linux (Linux)

- Internal class broadcasting needs public and static (Programming)

- The Concept and Semantics of Java Memory Model (Programming)

- Hadoop 1 and 2.x installation notes (Server)

- ethtool command Detailed (Linux)

- Ubuntu Tutorial - Manually install Oracle Java JDK 8 (Linux)

- Import and export myloader accelerate mydumper (Database)

- How to track performance issues when using the Unity Game Development Android (Programming)

- Java multi-threaded communications pipeline flow (Programming)

- Install MATE desktop environment adjustment tools Mate Tweak 3.3.6 (Linux)

- Java regular expressions examples (Programming)

- Linux system ARP attack solution (Linux)

- 24 Docker recommendations (Linux)

- httpd-2.4 feature (Server)

- Oracle Sql Loader tool has shown signs (Database)

- MySQL Authorized a recovered (Database)

- Git Experience Sharing - Using a remote repository (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:      
 
- Ubuntu 14.04 install Nmap 6.46.1 (Linux)
- GlusterFS distributed storage deployment (Server)
- JavaScript file loader LABjs API Explanation (Programming)
- Using Libreoffice under ubuntu (Linux)
- ctop: monitor container performance Linux command line artifact (Linux)
- Install Xshell on Mac OS X (Linux)
- MySQL my.cnf sql_mode schema modifications (Database)
- Linux operating system must know the security command (Linux)
- Taught you how to build your own VPS server (Server)
- Ruby and Python simple comparison (Programming)
- How linux network security configuration (Linux)
- Linux System Getting Started tutorial: Ubuntu desktop using the command line to change the system proxy settings (Linux)
- Radius server setup under CentOS (Server)
- About DataGuard three protected mode test (Database)
- How UTorrent download the ISO image from the command line (Linux)
- Linux Tutorial ---- fundamentals of data traffic redirection (Linux)
- Install the latest Eclipse IDE in Ubuntu (Linux)
- Linux performance monitoring and common commands Introduction (Linux)
- Processor in protected mode of protection (Linux)
- Firewall Configuration Red Hat Enterprise Linux 4 (Linux)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.