yzx99

导航

 

改后方案还有一个问题:索引器是用文本,这样,如果程序员不小心写错一个配置选项,系统无法在编译阶段就发现,所以想把代码由:
string sTemp = oXMJH2["XM1"].Value;
改为
string sTemp = oXMJH2.XM1.Value;

ClsXMJH2.cs 代码如下:

using System.Collections.Generic;

namespace WindowsFormsApplication12
{
    class ClsXMJH2
    {
        public ClsXM XM1 = new ClsXM("XM1");
        public ClsXM XM2 = new ClsXM("XM2");

        List<ClsXM> oXM = new List<ClsXM>();

        public ClsXMJH2()
        {
            foreach (System.Reflection.FieldInfo obj1 in this.GetType().GetFields())
            {
                if (obj1.FieldType.Name == XM1.GetType().Name)
                {
                    oXM.Add(obj1.GetValue(this) as ClsXM);
                }
            }
        }

        public void GetAllValue()
        {
            foreach (ClsXM oTempXM in oXM)
            {
                oTempXM.GetValue();
            }
        }

        public void SaveAllValue()
        {
            foreach (ClsXM oTempXM in oXM)
            {
                oTempXM.SaveValue();
            }
        }
    }
}

Form1.cs 的代码就可以改为:

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication12
{
    public partial class Form1 : Form
    {
        ClsXMJH2 oXMJH2 = new ClsXMJH2();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            oXMJH2.GetAllValue();
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            oXMJH2.SaveAllValue();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string sTemp = oXMJH2.XM1.Value;
            MessageBox.Show(sTemp);
            oXMJH2.XM2.Value = DateTime.Now.ToString("hh:mm:ss");
        }
    }
}

到顶了,目前想不到更好的优化方面了。

posted on 2013-10-18 02:02  yzx99  阅读(352)  评论(0编辑  收藏  举报