c# - 值为 null,但检查返回其他值?

标签 c# debugging null unity3d

编辑:This explains everything! - 当您销毁它们时,Unity 在您的 UnityEngine.Objects 周围创建托管假包装器。这意味着如果您销毁 UEObject,C# 包装器可能仍然不为空。 == 以自定义方式实现,因此当您销毁 UEObject 时,检查 == null 将返回 true。这显然不适用于泛型。


这真的快把我逼疯了。我这里有这个方法:

public static void AssertNotNullAfterAssignment<T>(ref T value, Func<T> get, string msg) where T : class
{
    if (value == null)
        value = get();
    if (value == null)
        throw new NullReferenceException(msg);
}

我在开头为 null 的引用上调用它:

AssertNotNullAfterAssignment(ref fovMeshFilter, GetComponent<MeshFilter>, "fovMeshFilter");

真正疯狂的是,检查 if (value == null) 返回 false!即使值为 null!

Here's a short video我展示了这个。

有趣的是,如果我将方法代码复制/粘贴到我在(OnEnable 中)使用它的方法中,它就可以工作(几乎是我在视频中的 OnEnable 中评论的内容)

所以...如果它在断言方法之外但在内部则不行。我还尝试了 Equals 而不是 == 但相同。

有人知道发生了什么事吗?

编辑:这是 OnEnable,我从中调用断言的方法:

private void OnEnable()
{
    //if (fovMeshFilter== null)
    //  fovMeshFilter= GetComponent<MeshFilter>();
    //if (fovMeshFilter== null)
    //  throw new NullReferenceException("fovMeshFilter");

    AssertNotNullAfterAssignment(ref fovMeshFilter, GetComponent<MeshFilter>, "fovMeshFilter");
}

如果我使用未注释的行,它会按预期工作,但断言方法出于某些天知道为什么的原因不起作用。

Edit1:我们这里有什么?

enter image description here

编辑2:

enter image description here

编辑3:

因此,在这个很棒的社区的大力帮助和几次测试之后,我找到了解决方案。 我可以发誓这是我最先尝试的事情之一!你必须相信我! XD - 只是使用 .Equals 而不是 == - 就像@Edin 在他的回答中显示的那样,做一个 == 在通用对象上似乎调用了 System.Object== - 但是,调用 .Equals 应该始终解析为正确的重载等于。我不知道为什么会这样,我的意思是,为什么“==”不能解析为正确的重载?

最佳答案

您的对象不为空。在调试器中看到 null 并不意味着它是空引用。如果您可以在调试器中展开对象,那么它肯定不是空引用。这可能是 DebuggerDisplay 字符串或 ToString() 方法在您的情况下返回 "null" 但这与空引用不同。

拿这个类举例

[DebuggerDisplay("null")]
class A { }

A 的实例a

var a = new A();

当您用鼠标滑过它时,将在调试器中显示为 a|null

你的 T 肯定有这个属性。返回 "null"ToString() 覆盖将在字符串周围添加额外的大括号:a|{null}

编辑:

看到你的 Edit2,让我意识到你真正的问题可能是什么。您的 == 运算符很可能被覆盖,因此在某些情况下,当您将类的实例与 null 进行比较时,它会返回 true,尽管实例本身不是 null。然而,在泛型方法中,在编译时并不知道你的参数是什么类型,因此最通用的运算符将应用于你的 T 参数,这就是引用比较。这在以下线程中有很好的描述:C# generics class operators not working

这可能就是为什么 OnEnable() 中的比较起作用,而您的通用方法中的比较不起作用的原因。

但是这还没有完全证明,因为我看不到你的代码。但你可以验证这一点。

下面是运算符与泛型结合使用时无法正常工作的完整示例:

class Person
{
  public string Name { get; set; }            

  public static bool operator ==(Person left, Person right)
  {
    // we consider Person null when it either has no Name or it is a null reference.
    if (object.ReferenceEquals(null, left) || left.Name == null)
      return object.ReferenceEquals(null, right);
    return left.Equals(right);
  }

  public static bool operator !=(Person left, Person right) { return !(left == right); }
  // Note that == and != operators should ideally be implemented in combination of Equals() override.
  // This is only for making an example for this question
}

private static bool IsNull<T>(T val)
{
  return val == null;
}

static void Main(string[] args)
{
  Person person = new Person();
  //this will display: person == null => True
  Console.WriteLine("person == null => {0}", person == null);
  //this will display: IsNull<Person>(person)=> False
  Console.WriteLine("IsNull<Person>(person)=> {0}", IsNull(person));
}

关于c# - 值为 null,但检查返回其他值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23995506/

相关文章:

c# - 如何在不单击一次和任何其他第三方工具的情况下在 wpf 中添加软件更新程序?

c - c中的二进制搜索算法

c++ - 为什么按照 ISO C++ 避免在 new 之后检查 null

python - 从文件中读取空终止(C 风格)字符串的简洁方法?

Azure 日志查询显示空值

c# - mvc 添加查询字符串变量

匿名类型的 C# 类型转换异常,为什么?

c# - 如何将新的DataRow 添加到DataTable 中?

debugging - 80386实模式模拟器

python - 告诉 pycharm 在 step in 期间跳过或跳过函数