无法从dicitonary的Object访问值 [英] Cannot access value from Object of dicitonary
本文介绍了无法从dicitonary的Object访问值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经为词典声明了一个GLobal类
使用系统;
使用 System.Data;
使用 System.Configuration;
使用 System.Linq;
使用 System.Web;
使用 System.Web.Security;
使用 System.Web.UI;
使用 System.Web.UI.HtmlControls;
使用 System.Web.UI.WebControls;
使用 System.Web.UI.WebControls.WebParts;
使用 System.Xml.Linq;
使用 System.Collections.Generic;
/// < 摘要 >
/// GeneralClass的汇总说明
/// < / summary >
public class 全球
{
public static 字典<字符串,对象> Dictionary_common_setting = new Dictionary< string,object>();
public 全球()
{
//
// TODO:添加构造函数逻辑这里
//
}
}
另一个存储字典值的类
使用系统;
使用 System.Data;
使用 System.Configuration;
使用 System.Linq;
使用 System.Web;
使用 System.Web.Security;
使用 System.Web.UI;
使用 System.Web.UI.HtmlControls;
使用 System.Web.UI.WebControls;
使用 System.Web.UI.WebControls.WebParts;
使用 System.Xml.Linq;
/// < 摘要 >
/// common_setting的摘要说明
/// < / summary >
public class dict_common_setting
{
public string 名称,值;
public dict_common_setting()
{
}
}
在字典中存储值
public < span class =code-keyword> bool UpdCommonSetting()
{
try
{
mycon.Open();
MySqlCommand com = new MySqlCommand( 选择名称,来自common_setting的值,mycon);
dr = com.ExecuteReader();
while (dr.Read())
{
string name = dr.GetString( 0 );
string value = dr.GetString( 1 跨度>);
dict_common_setting commsett = new dict_common_setting();
commsett.Value = value ;
Global.Dictionary_common_setting.Add(name,commsett);
}
dr.Close();
mycon.Close();
}
catch (例外情况)
{
mycon.Close();
}
返回 true ;
}
这是问题
if (!IsPostBack)
{
object ObjGetImgPath ;
function_class func = new function_class();
func.UpdCommonSetting();
if (Global.Dictionary_common_setting.ContainsKey( imgpath))
{
ObjGetImgPath = Global.Dictionary_common_setting [ imgpath 跨度>];
}
ObjGetImgPath。 // 我在Object ObjGetImPath中获取了键和值,但无法从该对象访问它。
// 如何获取字符串ImgPath = ObjGetImgPath.valuee;
if (会话[ id] == < span class =code-keyword> null )
{
Response.Redirect( 〜/ AdminPanel / frmLogout.aspx跨度>);
}
}
解决方案
我怀疑你需要将对象强制转换为合适的类型,但更好解决方法是更改字典。
如果你不能那样做,那就试试
< pre lang =c#>objectdict_common_setting ObjGetImgPath;
...
ObjGetImgPath = (dict_common_setting) Global.Dictionary_common_setting [ imgpath跨度>];
或
ObjGetImgPath = Global.Dictionary_common_setting [ imgpath] as dict_common_setting ;
I have declare one GLobal class for Dictionary
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
/// <summary>
/// Summary description for GeneralClass
/// </summary>
public class Global
{
public static Dictionary<string, object> Dictionary_common_setting = new Dictionary<string, object>();
public Global()
{
//
// TODO: Add constructor logic here
//
}
}
Another class to store value of dictionary
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
/// <summary>
/// Summary description for common_setting
/// </summary>
public class dict_common_setting
{
public string Name, Value ;
public dict_common_setting()
{
}
}
Storing value in dictionary
public bool UpdCommonSetting()
{
try
{
mycon.Open();
MySqlCommand com = new MySqlCommand("select name,value from common_setting", mycon);
dr = com.ExecuteReader();
while (dr.Read())
{
string name = dr.GetString(0);
string value = dr.GetString(1);
dict_common_setting commsett = new dict_common_setting();
commsett.Value = value;
Global.Dictionary_common_setting.Add(name, commsett);
}
dr.Close();
mycon.Close();
}
catch (Exception ex)
{
mycon.Close();
}
return true;
}
Here is the problem
if (!IsPostBack)
{
object ObjGetImgPath;
function_class func = new function_class();
func.UpdCommonSetting();
if (Global.Dictionary_common_setting.ContainsKey("imgpath"))
{
ObjGetImgPath = Global.Dictionary_common_setting["imgpath"];
}
ObjGetImgPath. //I get the key and value in Object ObjGetImPath but unable to access it from that object.
//How to get in string ImgPath = ObjGetImgPath.valuee;
if (Session["id"] == null)
{
Response.Redirect("~/AdminPanel/frmLogout.aspx");
}
}
解决方案
I suspect you need to cast the object to the proper type, but a better solution is to change the dictionary.
If you can't do that, then try
objectdict_common_setting ObjGetImgPath; ... ObjGetImgPath = (dict_common_setting) Global.Dictionary_common_setting["imgpath"]; or ObjGetImgPath = Global.Dictionary_common_setting["imgpath"] as dict_common_setting;
这篇关于无法从dicitonary的Object访问值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文