Skip to content

Instantly share code, notes, and snippets.

@pinalbhatt
Created August 3, 2014 13:26
Show Gist options
  • Save pinalbhatt/f9651702439f895edf3d to your computer and use it in GitHub Desktop.
Save pinalbhatt/f9651702439f895edf3d to your computer and use it in GitHub Desktop.
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;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment