get different values using dynamic keyword

149 Views Asked by At

in my web application im trying to get appsettings and connection strings section valued from web.config. Im using dynamic keywork to get the section values. please see my code below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Dynamic;
using System.Collections.Specialized;
using System.Configuration;

namespace SessiontimeoutCshp
{
    public class AppSettingsWrapper:DynamicObject
    {
        private NameValueCollection _items;
        private ConnectionStringSettingsCollection _connections;
        public enum Configsections
        {
            Appsettings,
            Connectionstrings
        }

        private string configsections;

        public string ConfigSections
        {
            get { return configsections; }
            set { configsections = value; }
        }


        public AppSettingsWrapper(Configsections conEnum)
        {
            switch (conEnum)
            {
                case Configsections.Appsettings:
                    _items = ConfigurationManager.AppSettings;
                    break;
                case Configsections.Connectionstrings:                    
                    _connections = ConfigurationManager.ConnectionStrings;
                    break;
                default:
                    break;
            }

            //if (conEnum.Equals(Configsections.Appsettings))
            //{
            //    _items = (T)(object)ConfigurationManager.AppSettings;
            //}
            //if (conEnum.Equals(Configsections.Connectionstrings))
            //{
            //    _connections = (T)(object)ConfigurationManager.ConnectionStrings;
            //}
        }

        //private object GetSectionvalue<T>(T sectionReturn)        
        //{ 

        //}



        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            result = null;
            if (_items != null)
            {
                result = _items[binder.Name];
            }
            if (_connections != null)
            {
                result = _connections[binder.Name];
            }
            return result != null;
            //return base.TryGetMember(binder, out result);
        }

        public override bool TryConvert(ConvertBinder binder, out object result)
        {
            return base.TryConvert(binder, out result);
        }
    }
}

im accesssing the appsettingwrapper class in default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Collections.Specialized;


namespace SessiontimeoutCshp
{
    public partial class _Default : System.Web.UI.Page
    {
        public string str;
        protected void Page_Load(object sender, EventArgs e)
        {
           // int i = Convert.ToInt32(ConfigurationManager.AppSettings["Sessiontimeout"]);
          // object obj= ConfigurationManager.GetSection("membership");
            dynamic appSettings = new AppSettingsWrapper(AppSettingsWrapper.Configsections.Appsettings);


            //str = appSettings.SessionWarning;
           // int testImplicit = appSettings.SessionWarning;

            string str1 = LinkButton1.ClientID;
        }
    }
}

Here when i want to get values for appsettings section i need to instantiate the wrapper class with appsettings enum and in the next line if i want to get value of connection string section i need to instantiate again the appsettings wrapper class. But i want to get both the values using only one instance of appsettings wrapper class. I tried using properties but since appsettingwrapper class is inherited from dynamic object the properties declared are not available in the intellisense. Please let me know how to get values of both the sections using single instance of appsettingswrapper class. Please let me know if any further information required.

0

There are 0 best solutions below