大家好,我有两个类,并且我有两个方法,分别将这两个类传递给方法并检查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; }
}
方法1:
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);
}
方法2
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);
}
有什么方法可以将这两种方法合并为一个通用方法?
提前谢谢!
答案 0 :(得分:1)
将共享成员移动到基本类型
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);
}