Created
August 3, 2014 13:26
-
-
Save pinalbhatt/f9651702439f895edf3d to your computer and use it in GitHub Desktop.
C# Dynamic Object Nested Member Call
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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