使用动态关键字获取不同的值

时间:2014-04-30 09:11:10

标签: asp.net .net c#-4.0 dynamic-keyword

在我的网络应用程序中我试图从web.config获取appsettings和连接字符串部分。我使用动态关键字来获取部分值。请看下面的代码

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访问default.aspx.cs中的appsettingwrapper类

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;
        }
    }
}

这里当我想获取appsettings部分的值时,我需要使用appsettings枚举实例化包装类,如果我想获取连接字符串部分的值,我需要再次实例化appsettings包装类。但我想只使用appsettings包装类的一个实例来获取这两个值。我尝试使用属性,但由于appsettingwrapper类是从动态对象继承的,因此声明的属性在intellisense中不可用。请告诉我如何使用appsettingswrapper类的单个实例获取这两个部分的值。如果需要任何进一步的信息,请告诉我。

0 个答案:

没有答案