この Web サイトに関する提案に従って、次の方法を作成しました。
public static T? GetElementValue<T>(this XElement xElement, string s)
where T : struct, IComparable
{
var result = new Nullable<T>();
try
{
if (string.IsNullOrEmpty(s) || s.Trim().Length > 0 || xElement.IsEmpty)
return result;
var element = xElement.Element(s);
if (element == null || element.IsEmpty)
return result;
var conv = TypeDescriptor.GetConverter(typeof(T));
result = (T)conv.ConvertFrom(element.Value);
}
catch (Exception ex)
{
ex.Message.WriteAsError();
}
return result;
}
しかし、それを使用しようとすると、string
まだ次のエラーが発生します: 型 '文字列' は、ジェネリック型またはメソッドでパラメーター 'T' として使用するために、null 非許容値型である必要があります
誰かが理由を教えてくれますか?どうも