pinalbhatt
8/3/2014 - 1:26 PM

C# Dynamic Object Nested Member Call

C# Dynamic Object Nested Member Call

using System;
using System.Dynamic;
namespace WW.COM.Framework.WWConfiguration
{
    public class EnvSettings : DynamicObject
    {
        private string Category { get; set; }

        public EnvSettings()
        {
            Category = String.Empty;
        }
        public EnvSettings(string category)
        {
            if (!string.IsNullOrEmpty(category))
                Category = category;
            else
                Category = String.Empty;
        }
        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            string propName = binder.Name;
            if (!string.IsNullOrEmpty(Category))
            {
                propName = Category + "." + binder.Name;
            }                        
            result = EnvConfiguration.Settings[propName]; /*Code to get property value and will return null if property does not exists*/
            if (result == null)
            {
                result = new EnvSettings(propName); /*For Nested call*/
               
            }
            return result == null ? false : true;           
        }
    }    
}