使用这种代码:
public void UpdateCellFont(int id, string colName, Font font)
{
CellLocation location = new CellLocation(id, colName);
if (CellAppearances.ContainsKey(location))
{
CellAppearances[location].Font = font;
}
else
{
CellAppearance cell = new CellAppearance(font, _DefaultBackColor, _DefaultForeColor);
CellAppearances.Add(location, cell);
}
}
public void UpdateCellBackColor(int id, string colName, Color backColor)
{
CellLocation location = new CellLocation(id, colName);
if (CellAppearances.ContainsKey(location))
{
CellAppearances[location].BackColor = backColor;
}
else
{
CellAppearance cell = new CellAppearance(_DefaultFont, backColor, _DefaultForeColor);
CellAppearances.Add(location, cell);
}
}
public void UpdateCellForeColor(int id, string colName, Color foreColor)
{
CellLocation location = new CellLocation(id, colName);
if (CellAppearances.ContainsKey(location))
{
CellAppearances[location].ForeColor = foreColor;
}
else
{
CellAppearance cell = new CellAppearance(_DefaultFont, _DefaultBackColor, foreColor);
CellAppearances.Add(location, cell);
}
}
这些方法几乎都做同样的事情——每一个都会更新 Font、BackColor 或 ForeColor(或者如果字典中没有条目,它们会创建一个新条目。
当它们作用于强类型的 CellAppearance 时,如何减少此处的重复?
谢谢