DOBON.NETプログラミング道掲示板

■35580 / 1階層)  抽象クラスで実装したクラスの情報を知る
□投稿者/ とくま 一般人(4回)-(2024/02/20(Tue) 21:37:29)
  • アイコン
    No35578に返信(夏風さんの記事)
    数年前、同じようなこと考えて色々作り込んだ後、ジェネリックの部分だけ
    あれば事足りるんじゃないかという結論になったことがある。。。
        class MainClass
        {
            public static void Main()
            {
                const string strBasePath = @"C:\test\";
                string strForm1XML = Path.Combine(strBasePath, @"Form1Setting.xml");
                string strForm2XML = Path.Combine(strBasePath, @"Form2Setting.xml");
    
                clsGenericsTest<clsForm1Setting> obj1 = new clsGenericsTest<clsForm1Setting>(strForm1XML);
                clsForm1Setting objSaveItem1 = new clsForm1Setting();
                objSaveItem1.TimeStamp = DateTime.Now;
                obj1.SaveFile(objSaveItem1);
    
                clsGenericsTest<clsForm2Setting> obj2 = new clsGenericsTest<clsForm2Setting>(strForm2XML);
                clsForm2Setting objSaveItem2 = new clsForm2Setting();
                objSaveItem2.TimeStamp = DateTime.Now.AddDays(10);
                obj2.SaveFile(objSaveItem2);
            }
        }
        public class clsForm1Setting
        {
            public DateTime TimeStamp;
        }
        public class clsForm2Setting
        {
            public DateTime TimeStamp;
        }
        public class clsGenericsTest<T> where T : class, new()
        {
            private string _filePath;
            private clsGenericsTest() { }
            public clsGenericsTest(string FilePath) { _filePath = FilePath; }
            public void SaveFile(T item)
            {
                XmlSerializer serializer1 = new XmlSerializer(typeof(T));
                StreamWriter sw = new StreamWriter(_filePath, false, Encoding.GetEncoding("Shift_JIS"));
                serializer1.Serialize(sw, item);
                sw.Close();
            }
            public T ReadFile()
            {
                XmlSerializer serializer = new XmlSerializer(typeof(T));
                StreamReader sr = new StreamReader(_filePath, Encoding.GetEncoding("Shift_JIS"));
                T obj = (T)serializer.Deserialize(sr);
                sr.Close();
    
                return obj;
            }
        }

違反を報告
削除キー/

前の記事(元になった記事) 次の記事(この記事の返信)
←抽象クラスで実装したクラスの情報を知る /夏風 →Re[2]: 抽象クラスで実装したクラスの情報を知る /夏風
 
上記関連ツリー

Nomalアイコン 抽象クラスで実装したクラスの情報を知る / 夏風 (24/02/20(Tue) 08:27) #35578
Nomalアイコン Re[1]: 抽象クラスで実装したクラスの情報を知る / 魔界の仮面弁士 (24/02/20(Tue) 11:51) #35579
Nomalアイコン 抽象クラスで実装したクラスの情報を知る / とくま (24/02/20(Tue) 21:37) #35580 ←Now
  └Nomalアイコン Re[2]: 抽象クラスで実装したクラスの情報を知る / 夏風 (24/02/21(Wed) 08:11) #35581 解決み!

All 上記ツリーを一括表示 / 上記ツリーをトピック表示
 
上記の記事へ返信

Mode/  Pass/


- Child Tree -