, - , . , , .
? , TryParse:
public delegate bool TryParser<T>(string text, out T value);
, :
public bool TryParse<T>
(XElement element, string attributeName, TryParser<T> tryParser, out T value)
{
value = default(T);
if (
element.Attribute(attributeName) != null &&
!string.IsNullOrEmpty(element.Attribute(attributeName).Value)
)
{
string valueString = element.Attribute(attributeName).Value;
return tryParser(valueString, out value);
}
return false;
}
:
public bool TryParseInt(XElement element, string attributeName, out int value)
{
return TryParse<int>(element, attributeName, int.TryParse, out value);
}
public bool TryParseBool(XElement element, string attributeName, out bool value)
{
return TryParse<bool>(element, attributeName, bool.TryParse, out value);
}
.
, , where T : struct .NET. XML- TryParse .
source
share