C# 模拟未知键号的关联数组(如 PHP)
C# mimic associative array of unknown key-number (like in PHP)
是否有可能创建某事。像 PHP 中的关联数组?
我不打算用一些玩家数据创建游戏,但我可以很容易地用这种方式解释我想要的东西:
player["Name"] = "PName";
player["Custom"]["Gender"] = "Female";
player["Custom"]["Style"] = "S1";
player["Custom"]["Face"]["Main"] = "FM1";
player["Custom"]["Face"]["Eyes"] = "FE1";
player["Custom"]["Height"] = "180";
另外长度必须是动态,我不知道有多少键将会有:
player["key1"]["key2"]=value
player["key1"]["key2"]["key3"]["key4"]...=value
我需要的是……我可以这样称呼:
string name = player["Name"];
string gender = player["Custom"]["Gender"];
string style = player["Custom"]["Style"];
string faceMain = player["Custom"]["Face"]["Main"];
string faceEyes = player["Custom"]["Face"]["Eyes"];
string height = player["Custom"]["Height"];
或者在某种程度上类似于此。
到目前为止我尝试了什么:
Dictionary<string, Hashtable> player = new Dictionary<string, Hashtable>();
player["custom"] = new Hashtable();
player["custom"]["Gender"] = "Female";
player["custom"]["Style"] = "S1";
但问题从这里开始(仅适用于 2 个键):
player["custom"]["Face"] = new Hashtable();
player["Custom"]["Face"]["Main"] = "FM1";
C# 是强类型的,因此复制这种行为似乎并不容易。
A "possibility" :
public class UglyThing<K,E>
{
private Dictionary<K, UglyThing<K, E>> dicdic = new Dictionary<K, UglyThing<K, E>>();
public UglyThing<K, E> this[K key]
{
get
{
if (!this.dicdic.ContainsKey(key)) { this.dicdic[key] = new UglyThing<K, E>(); }
return this.dicdic[key];
}
set
{
this.dicdic[key] = value;
}
}
public E Value { get; set; }
}
用法:
var x = new UglyThing<string, int>();
x["a"].Value = 1;
x["b"].Value = 11;
x["a"]["b"].Value = 2;
x["a"]["b"]["c1"].Value = 3;
x["a"]["b"]["c2"].Value = 4;
System.Diagnostics.Debug.WriteLine(x["a"].Value); // 1
System.Diagnostics.Debug.WriteLine(x["b"].Value); // 11
System.Diagnostics.Debug.WriteLine(x["a"]["b"].Value); // 2
System.Diagnostics.Debug.WriteLine(x["a"]["b"]["c1"].Value); // 3
System.Diagnostics.Debug.WriteLine(x["a"]["b"]["c2"].Value); // 4
是否有可能创建某事。像 PHP 中的关联数组? 我不打算用一些玩家数据创建游戏,但我可以很容易地用这种方式解释我想要的东西:
player["Name"] = "PName";
player["Custom"]["Gender"] = "Female";
player["Custom"]["Style"] = "S1";
player["Custom"]["Face"]["Main"] = "FM1";
player["Custom"]["Face"]["Eyes"] = "FE1";
player["Custom"]["Height"] = "180";
另外长度必须是动态,我不知道有多少键将会有:
player["key1"]["key2"]=value
player["key1"]["key2"]["key3"]["key4"]...=value
我需要的是……我可以这样称呼:
string name = player["Name"];
string gender = player["Custom"]["Gender"];
string style = player["Custom"]["Style"];
string faceMain = player["Custom"]["Face"]["Main"];
string faceEyes = player["Custom"]["Face"]["Eyes"];
string height = player["Custom"]["Height"];
或者在某种程度上类似于此。
到目前为止我尝试了什么:
Dictionary<string, Hashtable> player = new Dictionary<string, Hashtable>();
player["custom"] = new Hashtable();
player["custom"]["Gender"] = "Female";
player["custom"]["Style"] = "S1";
但问题从这里开始(仅适用于 2 个键):
player["custom"]["Face"] = new Hashtable();
player["Custom"]["Face"]["Main"] = "FM1";
C# 是强类型的,因此复制这种行为似乎并不容易。
A "possibility" :
public class UglyThing<K,E>
{
private Dictionary<K, UglyThing<K, E>> dicdic = new Dictionary<K, UglyThing<K, E>>();
public UglyThing<K, E> this[K key]
{
get
{
if (!this.dicdic.ContainsKey(key)) { this.dicdic[key] = new UglyThing<K, E>(); }
return this.dicdic[key];
}
set
{
this.dicdic[key] = value;
}
}
public E Value { get; set; }
}
用法:
var x = new UglyThing<string, int>();
x["a"].Value = 1;
x["b"].Value = 11;
x["a"]["b"].Value = 2;
x["a"]["b"]["c1"].Value = 3;
x["a"]["b"]["c2"].Value = 4;
System.Diagnostics.Debug.WriteLine(x["a"].Value); // 1
System.Diagnostics.Debug.WriteLine(x["b"].Value); // 11
System.Diagnostics.Debug.WriteLine(x["a"]["b"].Value); // 2
System.Diagnostics.Debug.WriteLine(x["a"]["b"]["c1"].Value); // 3
System.Diagnostics.Debug.WriteLine(x["a"]["b"]["c2"].Value); // 4