Click here to Skip to main content
15,893,622 members
Articles / Programming Languages / C# 4.0

Adventures with C# 4.0 dynamic - ExpandoObject, ElasticObject, and a Twitter client in 10 minutes

Rate me:
Please Sign up or sign in to vote.
4.89/5 (64 votes)
29 Mar 2010CPOL7 min read 210K   4.4K   144  
Explores the dynamic features in C# 4.0, and a few cool things you can do with the same.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AmazedSaint.Elastic.Lib
{
    public class SimpleHierarchyWrapper : IElasticHierarchyWrapper
    {
        private Dictionary<string, ElasticObject> attributes = new Dictionary<string, ElasticObject>();
        private Dictionary<string, List<ElasticObject>> elements = new Dictionary<string, List<ElasticObject>>();

        #region IElasticHierarchyWrapper<ElasticObject> Members

        public IEnumerable<KeyValuePair<string, ElasticObject>> Attributes
        {
            get { return attributes; }
        }

        public bool HasAttribute(string name)
        {
            return attributes.ContainsKey(name);
        }

        public ElasticObject Attribute(string name)
        {
            if (HasAttribute(name))
                return attributes[name];
            else
                return null;
        }

        public ElasticObject Element(string name)
        {
           return Elements.FirstOrDefault(item => item.InternalName == name);
        }

        public IEnumerable<ElasticObject> Elements
        {
            get
            {
                var result = from list in elements
                             from item in list.Value
                             select item;
                return result;
            }
        }

        public void AddAttribute(string key, ElasticObject value)
        {
            attributes.Add(key, value);
        }

        public void RemoveAttribute(string key)
        {
            attributes.Remove(key);
        }

        public void AddElement(ElasticObject element)
        {
            if (!elements.ContainsKey(element.InternalName))
            {
                elements[element.InternalName] = new List<ElasticObject>();
            }
            elements[element.InternalName].Add(element);
        }

        public void RemoveElement(ElasticObject element)
        {
            if (elements.ContainsKey(element.InternalName))
            {
                if (elements[element.InternalName].Contains(element))
                    elements[element.InternalName].Remove(element);
            }
        }

        private object internalContent;
        public object InternalContent
        {
            get
            {
                return internalContent;
            }
            set
            {
                internalContent = value;
            }
        }

        private object internalValue;
        public object InternalValue
        {
            get
            {
                return internalValue;
            }
            set
            {
                internalValue = value;
            }
        }

        private string internalName;
        public string InternalName
        {
            get
            {
                return internalName;
            }
            set
            {
                internalName = value;
            }
        }

        ElasticObject internalParent;
        public ElasticObject InternalParent
        {
            get
            {
                return internalParent;
            }
            set
            {
                internalParent = value;
            }
        }

        public void SetAttributeValue(string name, object obj)
        {
            attributes[name].InternalValue = obj;
        }

        public  object GetAttributeValue(string name)
        {
            return attributes[name].InternalValue;
        }


        #endregion

        
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
India India
Architect, Developer, Speaker | Wannabe GUT inventor & Data Scientist | Microsoft MVP in C#

Comments and Discussions