Created
May 31, 2013 10:18
-
-
Save ielcoro/5684115 to your computer and use it in GitHub Desktop.
Dynamic Configuration Example
This file contains hidden or 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
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; | |
} | |
} |
This file contains hidden or 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
public interface IDomainConfigurationModel | |
{ | |
string MyConfigValue { get; } | |
} |
This file contains hidden or 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
[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