如果以下方法中的条件具有相似的模式,那么所有这些都有任何想法可以提出一种减少此方法重复的常用方法吗?
有些情况我们想要所有的版本,在某些情况下我们只想要特定的版本。
我尝试使用switch case。
public override Func<JObject, dynamic, string> VersionMethod => (jobject, parameters) =>{
bool hasValidObject = false;
if (jobject["swVersion"] != null)
{
_livetv.SoftwareVersion = new VersionInfo(jobject["swVersion"].Value<string>());
hasValidObject = true;
}
if (jobject["hwVersion"] != null)
{
_livetv.HardwareVersion = new VersionInfo(jobject["hwVersion"].Value<string>());
hasValidObject = true;
}
if (jobject["ltvVersion"] != null)
{
_livetv.LTV2Version = new VersionInfo(jobject["ltvVersion"].Value<string>());
hasValidObject = true;
}
if (jobject["ltv3Version"] != null)
{
_livetv.LTV3Version = new VersionInfo(jobject["ltv3Version"].Value<string>());
hasValidObject = true;
}
if (jobject["cricVersion"] != null)
{
_livetv.KAVersion = new VersionInfo(jobject["cricVersion"].Value<string>());
hasValidObject = true;
}
if (jobject["bbVersion"] != null)
{
_livetv.BasebandVersion = new VersionInfo(jobject["bbVersion"].Value<string>());
hasValidObject = true;
}
if (hasValidObject)
{
return GenerateSuccessful();
}
return GenerateUnsuccessful(
"Unable to parse version from request, try again.");
};
答案 0 :(得分:0)
我创建了一个像这样的方法:
public VersionInfo GetVersionInfo(JObject jobject, string jObjectField, ref bool found)
{
if(jobject[jObjectField] != null)
{
_livetv.SoftwareVersion = new VersionInfo(jobject[jObjectField].Value<string>());
found = true;
}
else
{
return null;
}
}
然后这样称呼:
public Func<JObject, dynamic, string> VersionMethod = (jobject, parameters) =>
{
bool hasValidObject = false;
_livetv.SoftwareVersion = GetVersionInfo(jobject, "swVersion", ref hasValidObject);
_livetv.HardwareVersion = GetVersionInfo(jobject, "hwVersion", ref hasValidObject);
_livetv.LTV2Version = GetVersionInfo(jobject, "ltvVersion", ref hasValidObject);
_livetv.LTV3Version = GetVersionInfo(jobject, "ltv3Version", ref hasValidObject);
_ivetv.KAVersion = GetVersionInfo(jobject, "cricVersion", ref hasValidObject);
_livetv.BasebandVersion = GetVersionInfo(jobject, "bbVersion", ref hasValidObject);
if (hasValidObject)
{
return GenerateSuccessful();
}
return GenerateUnsuccessful(
"Unable to parse version from request, try again.");
};
答案 1 :(得分:0)
我会尝试类似的事情:
private VersionInfo GetVersionInfo(JToken token)
{
if (token != null)
return new VersionInfo(token.Value<string>());
else
return null;
}
public override Func<JObject, dynamic, string> VersionMethod => (jobject, parameters) =>
{
bool hasValidObject = false;
hasValidObject |= (_livetv.SoftwareVersion = GetVersionInfo(jobject["swVersion"])) != null;
hasValidObject |= (_livetv.HardwareVersion = GetVersionInfo(jobject["hwVersion"])) != null;
hasValidObject |= (_livetv.LTV2Version = GetVersionInfo(jobject["ltvVersion"])) != null;
hasValidObject |= (_livetv.LTV3Version = GetVersionInfo(jobject["ltv3Version"])) != null;
// <<<snip>>>
if (hasValidObject)
return GenerateSuccessful();
else
return GenerateUnsuccessful(
"Unable to parse version from request, try again.");
}