键值的转换
using System;
using System.Collections;
namespace demo1
{
class Foo
{
enum Bar{ 前= 1, 后, 左, 右 };
Hashtable ht;
Hashtable newht;
// 初始化码表对应关系
public void Load()
{
Hashtable rt = new Hashtable();
rt[4] = "right";
rt[3] = "back";
rt[2] = "left";
rt[1] = "foward";
ht = rt;
}
// 转换成新的对应关系
public void Save()
{
newht = new Hashtable();
foreach(DictionaryEntry de in ht)
{
Console.WriteLine(string.Format("key: {0} value: {1}", de.Key, de.Value));
if(Enum.IsDefined(typeof(Bar), de.Key)){
Bar bar = (Bar)de.Key;
Console.WriteLine(string.Format("枚举 -> {0}", bar.ToString()));
newht[bar.ToString()] = de.Value;
}
}
}
public void Print()
{
// 删除一对
Bar bar = Bar.后;
//newht[bar.ToString()] = "";
newht.Remove(bar.ToString());
Console.WriteLine("\n新对应关系");
foreach(DictionaryEntry de in newht)
{
Console.WriteLine(string.Format("key: {0} value: {1}", de.Key, de.Value));
}
}
}
class MainClass
{
public static void Main (string[] args)
{
Foo foo = new Foo();
foo.Load();
foo.Save();
foo.Print();
}
}
}