Skip to content

Instantly share code, notes, and snippets.

@ielcoro
Created May 31, 2013 10:18
Show Gist options
  • Save ielcoro/5684115 to your computer and use it in GitHub Desktop.
Save ielcoro/5684115 to your computer and use it in GitHub Desktop.
Dynamic Configuration Example
class ConfigurationStore : DynamicObject
{
public static TModel GetStore<TModel>()
{
return (dynamic)new ConfigurationStore();
}
private object GetConfigurationValue(string name)
{
object value = null;
if (name == "MyConfigValue")
{
value = "Success!";
}
return value;
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
result = GetConfigurationValue(binder.Name);
return (result != null);
}
public override bool TryConvert(ConvertBinder binder, out object result)
{
result = Impromptu.DynamicActLike(this, binder.ReturnType);
return true;
}
}
public interface IDomainConfigurationModel
{
string MyConfigValue { get; }
}
[TestClass]
public class UnitTest1
{
[TestMethod]
public void GetDynamicConfig()
{
dynamic config = new ConfigurationStore();
Assert.AreEqual("Success!", config.MyConfigValue);
}
[TestMethod]
public void TestDynamicCast()
{
//IDomainConfigurationModel config = (dynamic)new ConfigurationStore();
var config = ConfigurationStore.GetStore<IDomainConfigurationModel>();
Assert.AreEqual("Success!", config.MyConfigValue);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment