高手进来解释一下

klan klan 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我今天读了以下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这里就不要解释了,谢谢
...全文

相关推荐

评论 3
收藏 已收藏
分享
返回顶部
klan
C# 111,128 成员 642,536 内容