高手进来解释一下

klan 2007-08-22 02:27:36
我今天读了以下2段代码,有所不清楚,望告诉作答.谢谢

using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;

namespace KPIReportsApp
{
[Serializable]
public abstract class Settings<T>
{
protected abstract T GetDefault();

protected abstract string GetFilename();

protected T Load()
{
if (File.Exists(GetFilename()))
{
BinaryFormatter bf = new BinaryFormatter();
using (FileStream fs = new FileStream(GetFilename(), FileMode.OpenOrCreate))
{
return (T) bf.Deserialize(fs);
}
}
return GetDefault();
}

public void Save()
{
BinaryFormatter bf = new BinaryFormatter();
using (FileStream fs = new FileStream(GetFilename(), FileMode.OpenOrCreate))
{
bf.Serialize(fs, this);
}
}
}
}






using System;
using System.Collections.Generic;
using System.Text;

namespace KPIReportsApp
{
[Serializable]
public class DailyReportFormSettings : Settings<DailyReportFormSettings>
{
public static DailyReportFormSettings Current()
{
return new DailyReportFormSettings().Load();
}

protected override string GetFilename()
{
return "DailyReportFormSettings.cfg";
}

protected override DailyReportFormSettings GetDefault()
{
DailyReportFormSettings def = new DailyReportFormSettings();
def.Cell1 = "D15";
return def;
}

private string cell1;

public string Cell1
{
get { return cell1; }
set { cell1 = value; }
}
}
}

这2段代码的作用是为了保存config而写的.
只是有些基础知识不是很懂.
在定义类的时候用<T>表示什么? 另外,这种方式的定义有什么好处?
Serializable这里就不要解释了,谢谢
...全文
86 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
klan 2007-08-22
  • 打赏
  • 举报
回复
不错不错
kinglht 2007-08-22
  • 打赏
  • 举报
回复
http://dev.yesky.com/msdn/494/2437994.shtml
楼主可以参考一下!
真相重于对错 2007-08-22
  • 打赏
  • 举报
回复
范型 , 查msdn

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧