// 名簿管理用データクラス // Copyright (c) 2002 Tsutomu Saito All Rights Reserved using System; using System.Collections; using System.IO; using System.Runtime; using System.Runtime.Serialization.Formatters.Soap; namespace NameList { /// 名簿データ [Serializable] class NameData { private int id; private string name; private string tel; private string email; /// 順番 public int ID {get{return id;} set{id = value;}} /// 名前 public string Name {get{return name;} set{name = value;}} /// 電話番号 public string Tel {get{return tel;} set{tel = value;}} /// e-mailアドレス public string Email {get{return email;} set{email = value;}} /// コンストラクタ public NameData(string s1,string s2,string s3) { id = s_a.Count+1; name = s1; tel = s2; email = s3; } private class CompID : IComparer { public int Compare(object i,object j) {return ((NameData)i).id - ((NameData)j).id;} } private class CompName : IComparer { public int Compare(object i,object j) {return string.Compare(((NameData)i).name,((NameData)j).name);} } private class CompTel : IComparer { public int Compare(object i,object j) {return string.Compare(((NameData)i).tel,((NameData)j).tel);} } private class CompEmail : IComparer { public int Compare(object i,object j) {return string.Compare(((NameData)i).email,((NameData)j).email);} } /// 比較クラス public static IComparer Comparer(string s) { if (s == "ID") return new CompID(); if (s == "Name") return new CompName(); if (s == "Tel") return new CompTel(); if (s == "Email") return new CompEmail(); return null; } private static string s_fnam; private static ArrayList s_a = null; /// インスタンス取得 public static ArrayList Instance(System.Web.HttpServerUtility Server) { if (s_a == null) { lock (typeof(NameData)) { if (s_a == null) { ArrayList a = new ArrayList(); s_fnam = Server.MapPath("NameList.xml"); Stream stream = null; try { stream = File.Open(s_fnam,FileMode.Open); SoapFormatter formatter = new SoapFormatter(); formatter.AssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple; a = (ArrayList)formatter.Deserialize(stream); } catch { if (stream != null) stream.Close(); } s_a = a; } } } return s_a; } /// データ保存 public static void Save() { if (s_a == null) return; lock (s_a) { Stream stream = null; try { stream = File.Open(s_fnam,FileMode.Create); SoapFormatter formatter = new SoapFormatter(); formatter.AssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple; formatter.Serialize(stream,s_a); } catch {} finally { if (stream != null) stream.Close(); } } } } }