C# 可能的NullReferenceException

C# 可能的NullReferenceException,c#,nullreferenceexception,C#,Nullreferenceexception,例如,以下代码片段: private void LoadComments(Member member, XElement commentsXml) { member.Comments = (from comment in commentsXml.Descendants("comment") select new Comment() { ID = comme

例如,以下代码片段:

private void LoadComments(Member member, XElement commentsXml)
{
    member.Comments = (from comment in commentsXml.Descendants("comment")
                     select new Comment()
                     {
                         ID = comment.Element("id").Value,
                         Text = comment.Element("text").Value,
                         Date = comment.Element("date").Value,
                         Owner = comment.Element("user").Element("name").Value
                     }).ToList();
}
ReSharper警告我comment.Element行上可能存在NullReferenceException。果然,引发了异常


有没有关于如何避免这种情况的建议?如果它返回null,只返回一个空字符串,这可能吗?

可能-您需要将每个
comment.Element().Value
语句包装到函数中

我喜欢使用:

public string UnNull(object v)
{
   if (v == null) return "";
   return v.ToString();
}

至于最后一行,您需要特别小心,以确保
comment.Element(“user”)
不是
null
,如果是,则处理该情况。

这是可能的-您需要将每个
comment.Element().Value
语句包装到函数中

我喜欢使用:

public string UnNull(object v)
{
   if (v == null) return "";
   return v.ToString();
}

至于最后一行,您需要特别小心,以确保
comment.Element(“user”)
不是
null
,如果是空的话,请处理该情况。

我认为您只需要检查每个元素引用,因此:

private void LoadComments(Member member, XElement commentsXml)
{
    member.Comments = (from comment in commentsXml.Descendants("comment")
                     select new Comment()
                     {
                         ID = (comment.Element("id")==null)?"":comment.Element("id").Value,
                         Text = (comment.Element("text")==null)?"":comment.Element("text").Value,
                         Date = (comment.Element("date")==null)?"":comment.Element("date").Value,
                         Owner = (comment.Element("user")==null)?"":comment.Element("user").Element("name").Value
                     }).ToList();
}
最后一个有点弱,因为有两个节点确实需要检查,但是嵌套看起来有点不舒服,但这是唯一确定的方法

编辑----

作为一种扩展方法:

public static class MyExtensions
{

    public static string ValueSafe(this XElement target)
    {
        if (target==null)
            { return ""; }
        else
            { return target.Value; }
    }

}

然后,您可以将
.Value
替换为
.ValueSafe
,也就是说,您没有机会进行测试。

我认为您只需要检查每个元素引用,因此:

private void LoadComments(Member member, XElement commentsXml)
{
    member.Comments = (from comment in commentsXml.Descendants("comment")
                     select new Comment()
                     {
                         ID = (comment.Element("id")==null)?"":comment.Element("id").Value,
                         Text = (comment.Element("text")==null)?"":comment.Element("text").Value,
                         Date = (comment.Element("date")==null)?"":comment.Element("date").Value,
                         Owner = (comment.Element("user")==null)?"":comment.Element("user").Element("name").Value
                     }).ToList();
}
最后一个有点弱,因为有两个节点确实需要检查,但是嵌套看起来有点不舒服,但这是唯一确定的方法

编辑----

作为一种扩展方法:

public static class MyExtensions
{

    public static string ValueSafe(this XElement target)
    {
        if (target==null)
            { return ""; }
        else
            { return target.Value; }
    }

}

然后,您可以将
.Value
替换为
.ValueSafe
,也就是说,您没有机会进行测试。

我更喜欢为它提供一个扩展类:

public static class XElementExtension
{
   public static string GetValue(this XElement input)
   {
      if (input == null)
        return null;
      return input.Value as T;
   }

   public static XElement GetSubElement(this XElement element, string id)
   {
      if (element == null)
        return null;
      return element.Element(id);
   }
}
并将其用作:

ID = comment.Element("id").GetValue()

还有其他方式,如:


我更喜欢它的扩展类:

public static class XElementExtension
{
   public static string GetValue(this XElement input)
   {
      if (input == null)
        return null;
      return input.Value as T;
   }

   public static XElement GetSubElement(this XElement element, string id)
   {
      if (element == null)
        return null;
      return element.Element(id);
   }
}
并将其用作:

ID = comment.Element("id").GetValue()

还有其他方式,如:


一些扩展可能会有所帮助:

例子: 用法:
一些扩展可能有助于:

例子: 用法:
然后,由于字符串为空,您可能会在尝试获取不存在的元素值时遇到异常。然后,由于字符串为空,您可能会在尝试获取不存在的元素值时遇到异常。最好为如此长的行->comment.element(“id”)==null?创建一些类似extentin的包装器:“”:comment.element(“id”).Value您的第一个代码很难看,Steel您没有检查元素,在您的第一个示例中,
Owner=(comment.Element(“user”)==null)?“”:comment.Element(“user”).Element(“name”).Value
如果
Element(“name”)
为null,您将得到异常。@Saeed:难看是一个意见问题(尽管我在这里没有争议)而且真的需要考虑到行动的能力。不是每个人都理解扩展方法。至于嵌套节点比较,我注意到在我关于弱检查的评论中。@Lazarus,因为你写的是“不愉快的”,所以至少修改你的答案,你是这样说的…@Saeed:我真的不明白你的意思以及为什么它如此重要。最好为这么长的行->comment.Element(“id”)创建一些像extentin这样的包装器)==null)?“”:comment.Element(“id”).Value您的第一个代码很难看,而且您没有检查元素,在您的第一个示例中,
Owner=(comment.Element(“user”)==null)?“”:comment.Element(“user”).Element(“name”).Value
如果
元素(“name”)
为null,您将得到异常。@Saeed:难看是个意见问题(虽然我在这里没有异议)并且确实需要考虑OPs功能。不是每个人都理解扩展方法。至于嵌套节点比较,我在我关于弱检查的评论中已经指出。@Lazarus,正如你所写的,是“令人不快的”所以,至少修改一下你的答案,你是这么说的…@Saeed:我真的不明白你的意思以及为什么它如此重要。我甚至不知道它们的存在,但它们看起来很棒!每天学习新的东西。扩展方法解决了“可能的NullReferenceException”吗来自resharper的警告?@Vincent,我记不起resharper中发生了什么,你可以简单地测试一下。我甚至不知道它们存在,但它们看起来很棒!每天学习新的东西。扩展方法解决了“可能的NullReferenceException”吗来自resharper的警告?@Vincent,我记不起resharper中发生了什么,你可以简单地测试一下。可能重复的可能重复的