我有两种基本相同的方法,我想知道,如果有可能引入一个新参数来使这两种方法成为一个
方法:
public string CodeAnalysisEnabled(XElement propertyGroup, string groupName)
{
var codeAnalysis = (from doc in propertyGroup?.Descendants("RunCodeAnalysis") select doc).ToArray();
if (codeAnalysis.Length == 0)
{
return groupName + ": RunCodeAnalysis is missing.";
}
var allOk = codeAnalysis.All(n => n.Value.Equals("true", StringComparison.InvariantCultureIgnoreCase));
return allOk ? null : groupName + ": RunCodeAnalysis has wrong state.";
}
public string WarningsAsErrorsEnabled(XElement propertyGroup, string groupName)
{
var codeAnalysis = (from doc in propertyGroup?.Descendants("TreatWarningsAsErrors") select doc).ToArray();
if (codeAnalysis.Length == 0)
{
return groupName + ": TreatWarningsAsErrors is missing.";
}
var allOk = codeAnalysis.All(n => n.Value.Equals("true", StringComparison.InvariantCultureIgnoreCase));
return allOk ? null : groupName + ": TreatWarningsAsErrors has wrong state.";
}
答案 0 :(得分:2)
是的,只有一个区别可以作为参数传递:
public string MeaningfulMethodName(XElement propertyGroup, string groupName, string propertyName)
{
var codeAnalysis = (from doc in propertyGroup?.Descendants(propertyName) select doc).ToArray();
if (codeAnalysis.Length == 0)
{
return $"{groupName}: {propertyName} is missing.";
}
var allOk = codeAnalysis.All(n => n.Value.Equals("true", StringComparison.InvariantCultureIgnoreCase));
return allOk ? null : $"{groupName}: {propertyName} has wrong state.";
}
答案 1 :(得分:0)
这是怎么回事?
public string CodeAnalysisEnabled(XElement propertyGroup, string groupName)
{
return Check(propertyGroup, groupName, "RunCodeAnalysis");
}
public string WarningsAsErrorsEnabled(XElement propertyGroup, string groupName)
{
return Check(propertyGroup, groupName, "TreatWarningsAsErrors");
}
private static string Check(XElement propertyGroup, string groupName, string checkedParam)
{
var codeAnalysis = (from doc in propertyGroup?.Descendants(checkedParam) select doc).ToArray();
if (codeAnalysis.Length == 0)
{
return groupName + $": {checkedParam} is missing.";
}
var allOk = codeAnalysis.All(n => n.Value.Equals("true", StringComparison.InvariantCultureIgnoreCase));
return allOk ? null : groupName + $": {checkedParam} has wrong state.";
}