c#在类内循环所有方法并赋值

c#在类内循环所有方法并赋值,c#,winforms,ini,nini,C#,Winforms,Ini,Nini,我有一个包含所有按钮、标签的表单,我计划使用INI文件提供多语言支持。我创建了Lang类来保存值,如果用户更改了语言,则无需重新启动程序即可动态更新 我当前的代码: private void LangGet(string LangID) { IConfigSource Lng = new IniConfigSource(Globals.Path.FolderLang + "\\" + LangID + ".ini"); // Set var g = Lng.C

我有一个包含所有按钮、标签的表单,我计划使用INI文件提供多语言支持。我创建了Lang类来保存值,如果用户更改了语言,则无需重新启动程序即可动态更新

我当前的代码:

private  void  LangGet(string  LangID)
{
IConfigSource  Lng = new  IniConfigSource(Globals.Path.FolderLang  +  "\\"  +  LangID  +  ".ini");

  //  Set
  var  g = Lng.Configs["general"];
  Lang.Id.General.OK = g.GetString("OK");
  Lang.Id.General.Cancel = g.GetString("Cancel");
  Lang.Id.General.Error = g.GetString("Error");
  ...
  Lang.Id.General.lblStart = g.GetString("lblStart");
...
我想要的是代码更高效,但我不知道如何

IConfigSource Lng = new IniConfigSource(Globals.Path.FolderLang + "\\" + LangID + ".ini");
var g = Lng.Config["general"];
forloop ( ... )
{
    item = g.GetString(item);
}
如果更强大

IConfigSource Lng = new IniConfigSource(Globals.Path.FolderLang + "\\" + LangID + ".ini");
forloop ( ... )
{
    forloop ( ... )
    {
        TheName = Lng.Config[TheClass].GetString(TheName);
    }
}
将INI加载到变量后,控制文本获取变量值的时间

forloop ( control )
{
    forloop ( class )
    {
        if ( control name contain btn )
        {
            item.Text = Lng.Configs[TheClass].GetString(item.name?);
        }
        if ( control name contain lbl )
        {
            item.Text = Lng.Configs[TheClass].GetString(item.name?);
        }
        // So On...

我找到了我的答案。。。所以我回答我自己

使用Ini解析器而不是Nini

使用INI文件制作多语言,编译后允许对语言进行编辑,便于修改

相反,每个控件都要编写代码,使用循环来完成它们的工作

CreateLang();将使用控件名作为Ini键扫描所有窗体及其子窗体

LoadLang();将扫描所有窗体控件,一旦Ini键等于控件名,该值将应用于控件

在设计期间,所有控件必须更改为{0}或多行{0}{1}{2}

代码:


你需要使用反射。这使您可以枚举对象上的所有属性并分配值。我尝试了这一点,列出类内的所有属性成功了一半,问题在第二个forloop中。。。无法获得下一个类名,这里是我写的:LOL<代码>INI文件。。。我猜你打算在Windows 3.1中运行你的应用程序?因为它易于阅读。。。我以前用过XML,我请别人翻译它,大多数人都很困惑,我发送了INI示例,很容易阅读。。
    private void frmMain_Load(object sender, EventArgs e)
    {
        CreateLang(); // Create new empty language
        //LoadLang(); // Load language, GUI must use {0} {1} ... as place-holder
    }

    private void LoadLang()
    {
        var parser = new FileIniDataParser();
        IniData data = parser.ReadFile("eng.ini");

        Control ctrl = this;
        do
        {
            ctrl = this.GetNextControl(ctrl, true);

            if (ctrl != null)
                if (ctrl is Label ||
                    ctrl is Button ||
                    ctrl is TabPage ||
                    ctrl is CheckBox)
                    if (data["Root"][ctrl.Name].Contains('|')) // Character | donated by New-Line, \n
                        ctrl.Text = String.Format(ctrl.Text, data["Root"][ctrl.Name].Split('|'));
                    else
                        ctrl.Text = String.Format(ctrl.Text, data["Root"][ctrl.Name]);

        } while (ctrl != null);

    }

    private void CreateLang()
    {
        if (System.IO.File.Exists("eng.ini"))
            System.IO.File.WriteAllText("eng.ini", "");
        else
            System.IO.File.WriteAllText("eng.ini", "");

        var parser = new FileIniDataParser();
        IniData data = parser.ReadFile("eng.ini");

        data.Sections.AddSection("Info");
        data.Sections["Info"].AddKey("Name", "Anime4000");
        data.Sections["Info"].AddKey("Version", "0.1");
        data.Sections["Info"].AddKey("Contact", "fb.com/anime4000");

        string main = "Root";
        data.Sections.AddSection(main);

        Control ctrl = this;
        do
        {
            ctrl = this.GetNextControl(ctrl, true);

            if (ctrl != null)
                if (ctrl is Label ||
                    ctrl is Button ||
                    ctrl is TabPage ||
                    ctrl is CheckBox ||
                    ctrl is GroupBox)
                    data.Sections[main].AddKey(ctrl.Name, "");

        } while (ctrl != null);


        parser.WriteFile("eng.ini", data);
    }