c# - 将这两种方法合二为一
问题描述
大家好,我有两个类,我有两个方法,我将这两个类分别传递给方法并检查name
和NarrativeHTML
呈现项目符号列表。
public class LibraryHydronicEquipment : AEIMasterBase
{
public string Name { get; set; }
public HydronicEquipmentType? Type { get; set; }
[Column(TypeName = "jsonb")]
public List<HydronicSystemType> Systems { get; set; }
[Column(TypeName = "jsonb")]
public List<EquipmentProperty> Properties { get; set; }
public string NarrativeHTML { get; set; }
}
public class LibrarySteamEquipment : AEIMasterBase
{
public string Name { get; set; }
public SteamEquipmentType? Type { get; set; }
[Column(TypeName = "jsonb")]
public List<SteamSystemType> Systems { get; set; }
[Column(TypeName = "jsonb")]
public List<EquipmentProperty> Properties { get; set; }
public string NarrativeHTML { get; set; }
}
方法一:
private void BuildBulletedList(List<LibraryHydronicEquipment> libraryHydronicEquipments)
{
List<Run> labels = new List<Run>();
foreach (var equipmentItem in libraryHydronicEquipments)
{
if (!string.IsNullOrEmpty(equipmentItem.NarrativeHTML))
{
var htmlRuns = this.DocumentHtmlConverter.Parse(equipmentItem.NarrativeHTML)
.First()
.ChildElements
.Where(c => c is Run)
.Cast<Run>()
.Select(n => n.CloneNode(true));
labels.Add(new Run(htmlRuns));
}
else if (!string.IsNullOrEmpty(equipmentItem.Name))
{
labels.Add(new Run(new Text(equipmentItem.Name)));
}
}
BuildList(labels);
}
方法二
private void BuildBulletedList(List<LibrarySteamEquipment> librarySteamEquipments)
{
List<Run> labels = new List<Run>();
foreach (var equipmentItem in librarySteamEquipments)
{
if (!string.IsNullOrEmpty(equipmentItem.NarrativeHTML))
{
var htmlRuns = this.DocumentHtmlConverter.Parse(equipmentItem.NarrativeHTML)
.First()
.ChildElements
.Where(c => c is Run)
.Cast<Run>()
.Select(n => n.CloneNode(true));
labels.Add(new Run(htmlRuns));
}
else if (!string.IsNullOrEmpty(equipmentItem.Name))
{
labels.Add(new Run(new Text(equipmentItem.Name)));
}
}
BuildList(labels);
}
我正在调用这些方法,如下所示
if (hydronicSystem.Equipment.Source.Count != 0)
{
BuildBulletedList(hydronicSystem.Equipment.Source);
}
if (steamSystem.Equipment.Source.Count != 0)
{
BuildBulletedList(steamSystem.Equipment.Source);
}
更新 :
if (hydronicSystem.Equipment.Source.Count != 0)
{
BuildBulletedList(hydronicSystem.Equipment.Source);
}
if (hydronicSystem.Equipment.Distribution.Count != 0)
{
BuildBulletedList(hydronicSystem.Equipment.Distribution);
}
if (hydronicSystem.Equipment.Terminals.Count != 0)
{
BuildBulletedList(hydronicSystem.Equipment.Terminals);
}
有什么方法可以将这两种方法组合成一个通用方法?
提前致谢!!
解决方案
将共享成员移动到基本类型
public interface IRender {
string Name { get; set; }
string NarrativeHTML { get; set; }
}
并让类派生自该类型
public class LibraryHydronicEquipment : AEIMasterBase, IRender {
public string Name { get; set; }
public HydronicEquipmentType? Type { get; set; }
[Column(TypeName = "jsonb")]
public List<HydronicSystemType> Systems { get; set; }
[Column(TypeName = "jsonb")]
public List<EquipmentProperty> Properties { get; set; }
public string NarrativeHTML { get; set; }
}
public class LibrarySteamEquipment : AEIMasterBase, IRender {
public string Name { get; set; }
public SteamEquipmentType? Type { get; set; }
[Column(TypeName = "jsonb")]
public List<SteamSystemType> Systems { get; set; }
[Column(TypeName = "jsonb")]
public List<EquipmentProperty> Properties { get; set; }
public string NarrativeHTML { get; set; }
}
然后通过泛型重构该方法以依赖于该类型
private void BuildBulletedList<T>(IEnumerable<T> items) where T : IRender {
List<Run> labels = new List<Run>();
foreach (T item in items) {
if (!string.IsNullOrEmpty(item.NarrativeHTML)) {
var htmlRuns = DocumentHtmlConverter
.Parse(item.NarrativeHTML)
.First()
.ChildElements
.Where(c => c is Run)
.Cast<Run>()
.Select(n => n.CloneNode(true));
labels.Add(new Run(htmlRuns));
} else if (!string.IsNullOrEmpty(item.Name)) {
labels.Add(new Run(new Text(item.Name)));
}
}
BuildList(labels);
}
现在进行一次调用,连接列表,然后检查是否可以构建项目符号列表。
var source = hydronicSystem.Equipment.Source.Cast<IRender>();
var distribution = hydronicSystem.Equipment.Distribution.Cast<IRender>();
var terminals = hydronicSystem.Equipment.Terminals.Cast<IRender>();
var bullets = source.Concat(distribution).Concat(terminals);
if (bullets.Any()) {
BuildBulletedList(bullets);
}