I get how to use dynamic in C# 4.0, however, I'm not sure how to take something and make it dynamic-able (my technical term).
For example, instead of ConfigurationManager.AppSettings["blah"], how can I make a wrapper of sorts that will let me just use it like a dynamic: settings.Blah ?
You still need an entry point. However, from there the possibilities are quite flexible. This is an example idea to demonstrate how powerful dynamic dispatch can be:
public abstract class MyBaseClass
{
    public dynamic Settings
    {
        get { return _settings; }
    }
    private SettingsProxy _settings = new SettingsProxy();
    private class SettingsProxy : DynamicObject
    {
        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            var setting = ConfigurationManager.AppSettings[binder.Name];
            if(setting != null)
            {
                result = setting.ToString();
                return true;
            }
            result = null;
            return false;
        }
    }
}
                        New version with generics !!
public static  class WebConfiguration<T>
{
    public static dynamic Get {
        get { return _settings; }
    }
    static readonly SettingsProxy _settings = new SettingsProxy ();
    class SettingsProxy : DynamicObject
    {
        public override bool TryGetMember (GetMemberBinder binder, out object result)
        {
            var setting = ConfigurationManager.AppSettings [binder.Name];
            if (setting != null) {
                result = Convert.ChangeType (setting, typeof(T));
                return true;
            }
            result = null;
            return false;
        }
    }
}
Usage
WebConfiguration<int>.Get.UserNameLength
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With