我们如何通过其他方法的返回值?我希望得到的值format
,attr1
,attr2
,attr3
从GetFormat()
方法。但是不知何故我无法得到它。我想念的是什么?但是我确实初始化了它们,但是没有用。
public static bool GetFormat()
{
string format, attr1, attr2, attr3 = string.Empty;
try
{
string globalFormat = GetGlobalConfigStringValue(GLOBAL_CONFIG_ADVANCED_FULL_NAME);
XmlDocument doc = new XmlDocument();
doc.LoadXml(globalFormat.ToString());
XmlElement root = doc.DocumentElement;
XmlNodeList nodes = root.SelectNodes("//DisplayName");
format = nodes[0].SelectSingleNode("Format").InnerText.ToString();
attr1 = nodes[0].SelectSingleNode("Atrribute1").InnerText.ToString();
attr2 = nodes[0].SelectSingleNode("Atrribute2").InnerText.ToString();
attr3 = nodes[0].SelectSingleNode("Atrribute3").InnerText.ToString();
if (string.IsNullOrWhiteSpace(globalFormat) || string.IsNullOrWhiteSpace(attr1) || string.IsNullOrWhiteSpace(attr2))
{
return false;
}
}
catch (Exception)
{
return false;
}
return true;
}
public static string GetProfileDisplayName(string profileUID)
{
string format, attr1, attr2, attr3 = string.Empty;
if (GetFormat())
{
using (var context = GetAccessEntitiesContext())
{
var user = context.vw_Profile.Where(x => x.ProfileUID.Equals(profileUID)).FirstOrDefault();
return string.Format(format, user.GetType().GetProperty(attr1).GetValue(user), user.GetType().GetProperty(attr2).GetValue(user), user.GetType().GetProperty(attr3).GetValue(user));
}
}
else
{
format = "{0} {1}";
attr1 = "FirstName";
attr2 = "LastName";
using (var context = GetAccessEntitiesContext())
{
var user = context.vw_Profile.Where(x => x.ProfileUID.Equals(profileUID)).FirstOrDefault();
return string.Format(format, user.GetType().GetProperty(attr1).GetValue(user), user.GetType().GetProperty(attr2).GetValue(user));
}
}
}
实现此目的有多种方法,您必须重构您的GetFormat
方法。
public static GetFormatModel GetFormat()
{
GetFormatModel model = new GetFormatModel();
try
{
string globalFormat = GetGlobalConfigStringValue(GLOBAL_CONFIG_ADVANCED_FULL_NAME);
XmlDocument doc = new XmlDocument();
doc.LoadXml(globalFormat.ToString());
XmlElement root = doc.DocumentElement;
XmlNodeList nodes = root.SelectNodes("//DisplayName");
model.format = nodes[0].SelectSingleNode("Format").InnerText.ToString();
model.attr1 = nodes[0].SelectSingleNode("Atrribute1").InnerText.ToString();
model.attr2 = nodes[0].SelectSingleNode("Atrribute2").InnerText.ToString();
model.attr3 = nodes[0].SelectSingleNode("Atrribute3").InnerText.ToString();
if (string.IsNullOrWhiteSpace(globalFormat)
|| string.IsNullOrWhiteSpace(model.attr1)
|| string.IsNullOrWhiteSpace(model.attr2))
{
model.isSuccess = false;
return model;
}
}
catch (Exception)
{
model.isSuccess = false;
return model;
}
model.isSuccess = true;
return model;
}
// DTO
public class GetFormatModel
{
public bool isSuccess { get; set; }
public string format { get; set; } = string.Empty;
public string attr1 { get; set; } = string.Empty;
public string attr2 { get; set; } = string.Empty;
public string attr3 { get; set; } = string.Empty;
}
方式2)返回一个 generic Tuple
public static Tuple<bool, string, string, string, string> GetFormat()
{
Tuple<bool, string, string, string, string> tplGetFormat = default(Tuple<bool, string, string, string, string>);
try
{
string globalFormat = GetGlobalConfigStringValue(GLOBAL_CONFIG_ADVANCED_FULL_NAME);
XmlDocument doc = new XmlDocument();
doc.LoadXml(globalFormat.ToString());
XmlElement root = doc.DocumentElement;
XmlNodeList nodes = root.SelectNodes("//DisplayName");
tplGetFormat.Item2 = nodes[0].SelectSingleNode("Format").InnerText.ToString();
tplGetFormat.Item3 = nodes[0].SelectSingleNode("Atrribute1").InnerText.ToString();
tplGetFormat.Item4 = nodes[0].SelectSingleNode("Atrribute2").InnerText.ToString();
tplGetFormat.Item5 = nodes[0].SelectSingleNode("Atrribute3").InnerText.ToString();
if (string.IsNullOrWhiteSpace(globalFormat)
|| string.IsNullOrWhiteSpace(tplGetFormat.Item3)
|| string.IsNullOrWhiteSpace(tplGetFormat.Item4))
{
tplGetFormat.Item1 = false;
return tplGetFormat;
}
}
catch (Exception)
{
tplGetFormat.Item1 = false;
return tplGetFormat;
}
tplGetFormat.Item1 = true;
return tplGetFormat;
}
方式3)返回out
参数-
public static bool GetFormat(
out string format,
out string attr1,
out string attr2,
out string attr3
)
{
format = attr1 = attr2 = attr3 = string.Empty;
try
{
string globalFormat = GetGlobalConfigStringValue(GLOBAL_CONFIG_ADVANCED_FULL_NAME);
XmlDocument doc = new XmlDocument();
doc.LoadXml(globalFormat.ToString());
XmlElement root = doc.DocumentElement;
XmlNodeList nodes = root.SelectNodes("//DisplayName");
format = nodes[0].SelectSingleNode("Format").InnerText.ToString();
attr1 = nodes[0].SelectSingleNode("Atrribute1").InnerText.ToString();
attr2 = nodes[0].SelectSingleNode("Atrribute2").InnerText.ToString();
attr3 = nodes[0].SelectSingleNode("Atrribute3").InnerText.ToString();
if (string.IsNullOrWhiteSpace(globalFormat) || string.IsNullOrWhiteSpace(attr1) || string.IsNullOrWhiteSpace(attr2))
{
return false;
}
}
catch (Exception)
{
return false;
}
return true;
}
// Usage
string format = attr1 = attr2 = attr3 = string.Empty;
GetFormat(out format, out attr1, out attr2, out attr3);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句