【发布时间】:2020-09-18 17:34:08
【问题描述】:
好吧,我在一个基本的范围问题上苦苦挣扎(在我看来),我想从外部更新我的变量解决方案并保持它的活力(在 CProperties 类中)以将它传递到我的字典中,所以我保持相同的键不同的价值。
ps:我必须保持 Dictionary
pps:我来自 C++
这样做的正确方法是什么?
public static class CProperties
{
public static string solution { get; set; }
static public Dictionary<string, string> product = new Dictionary<string, string>()
{
{ "product_name", solution}
};
}
static void mycrazyFunc()
{
// I skip the Database connection and everything
DataTable dt = new DataTable();
dt.Load(cmd.ExecuteReader());
if (dt.Rows.Count > 0)
{
foreach (DataRow rows in dt.Rows)
{
dynamic JsonObj = new ExpandoObject();
if (!string.IsNullOrEmpty(rows["Product"].ToString()))
{
CProperties.solution = rows["Product"].ToString(); // Got the value !
JsonObj.custom_fields = CProperties.product; // Lost the value !
// {"product_name", "null"}
}
}
}
}
【问题讨论】:
-
您没有丢失价值,它从未被放入
product。当product被初始化时,solution就是null;这就是字典中的内容,而不是对solution属性的引用。更改solution不会更改product["product_name"](反正不是你在这里的方式)。 -
在你的情况下解释外部,外部类,外部过程,外部机器,你到底需要什么提供更多细节