我有一个 CParam 字典,其中注册为键,
CParam 有一个从外部文本文件中读取的字段,Description 用作在 HumanDesc 中读取的键。
文本文件是翻译文件,描述必须是字符串。像这样的东西
PLACE_HOLDER1 "First Place where things are put"
PLACE_HOLDER2 "Secod Place where things are put"
.....
我可以通过插入 Register as 并将其放在引号中轻松地做到这一点。但是有一个 100 的寄存器,它会很乏味(而且不是很优雅)。有没有办法让构造函数为我处理这个问题。
下面是我想要做的一个非常简化的例子:
using System;
using System.Collections.Generic;
namespace Var2String
{
public class CParam
{
public ushort Register;
public string Description;
public ushort Content;
public string HumanDesc;
public CParam(ushort t_Register, string t_Description, string t_HumanDesc, ushort DefaultVal)
{
Register = t_Register;
Description = t_Description;
Content = DefaultVal;
HumanDesc = t_HumanDesc;
}
};
static class Device1
{
public const ushort PLACE_HOLDER1 = 0x0123;
public const ushort PLACE_HOLDER2 = 0x0125;
public const ushort PLACE_HOLDER_SAME_AS_1 = 0x0123;
public static Dictionary<ushort, CParam> Registers;
static Device1()
{
Registers = new Dictionary<ushort, CParam>()
{
{PLACE_HOLDER1, new CParam(PLACE_HOLDER1,"PLACE_HOLDER1","Place One Holder",100)},
{PLACE_HOLDER2, new CParam(PLACE_HOLDER1,"PLACE_HOLDER2","Place Two Holder",200)}
};
/*
* Like to be able to do this
* And constructor CParam
Registers = new Dictionary<ushort, CParam>()
{
{PLACE_HOLDER1, new CParam(PLACE_HOLDER1,"Place One Holder",100)},
{PLACE_HOLDER2, new CParam(PLACE_HOLDER1,"Place Two Holder",200)}
};
*/
}
}
class Program
{
static private string LookUpTranslationFor(string Key)
{
string Translated = "Could not find Val for " + Key;
//This would read XML file use Key to get translation
return Translated;
}
static void Main(string[] args)
{
Console.WriteLine(Device1.Registers[Device1.PLACE_HOLDER1].HumanDesc);
Console.WriteLine(Device1.Registers[Device1.PLACE_HOLDER2].HumanDesc);
Device1.Registers[Device1.PLACE_HOLDER2].HumanDesc = LookUpTranslationFor(Device1.Registers[Device1.PLACE_HOLDER2].Description);
Console.WriteLine(Device1.Registers[Device1.PLACE_HOLDER2].HumanDesc);
Console.ReadKey(true);
}
}
}