Asp.net 使用dynamic关键字获取不同的值
在我的web应用程序中,我试图从web.config获取appsettings和connectionstrings部分的值。Im使用动态关键帧来获取截面值。请看下面我的代码Asp.net 使用dynamic关键字获取不同的值,asp.net,.net,c#-4.0,dynamic-keyword,Asp.net,.net,C# 4.0,Dynamic Keyword,在我的web应用程序中,我试图从web.config获取appsettings和connectionstrings部分的值。Im使用动态关键帧来获取截面值。请看下面我的代码 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Dynamic; using System.Collections.Specialized; using System.Confi
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);
}
}
}
在这里,当我想获取appsettings部分的值时,我需要用appsettings enum实例化包装类,在下一行中,如果我想获取connection string部分的值,我需要再次实例化appsettings包装类。但我只想使用appsettings包装类的一个实例来获取这两个值。我尝试使用属性,但由于appsettingwrapper类是从动态对象继承的,因此声明的属性在intellisense中不可用。请让我知道如何使用AppSettingsRapper类的单个实例获取这两个部分的值。如果需要更多信息,请告诉我。问题不是如何获取动态类的属性值。这是关于,如何使用继承自dynamic的类的对象的单个实例来获取webconfig值,而不是创建多个实例。通过字符串的答案链接获取值C dynamic属性在这里不相关。。。
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;
}
}
}