简体   繁体   English

使用null检查扩展“对象”比ReferenceEquals更具可读性

[英]Extend “object” with a null check more readable than ReferenceEquals

I tried to extend "object" to allow a more readable check if an object is null. 我试图扩展“对象”,以便在对象为空时允许更可读的检查。

Now, object.ReferenceEquals really checks for a null object, (the rare times it will not apply are since the operator == can be overridden. the object.Equals(null) method can also be overridden). 现在, object.ReferenceEquals 确实检查了一个空对象,(由于操作符==可以被覆盖,所以它不会应用的极少数时候。 object.Equals(null)方法也可以被覆盖)。

But the object.ReferenceEquals(null, obj); 但是object.ReferenceEquals(null, obj); is not too readable is it?... So, I thought, why not write an extension method to the System.object that will provide that check using object.IsNull(obj); 它是不是太可读了?...所以,我想,为什么不写一个扩展方法到System.object ,它将提供使用object.IsNull(obj);检查object.IsNull(obj);

I've tried: 我试过了:

public static class MyExtClass
{
    // the "IsNull" extension to "object"  
    public static bool IsNull(this object obj)
    {
        return object.ReferenceEquals(obj, null);
    }
}

public SomeOtherClass
{
     public static void TryUsingTheExtension()
     {
          object obj;

          // Why does this line fail? the extension method is not recognized
          // I get: 'object' does not contain a definition for "IsNull"
          bool itIsANull = object.IsNull(obj);
     }
}

What did I miss? 我错过了什么?

尝试

bool itIsANull = obj.IsNull();

Extension methods can be invoked only on instance and not on a class that they extend. 扩展方法只能在实例上调用,而不能在它们扩展的类上调用。 So this line of code bool itIsANull = object.IsNull(obj); 所以这行代码bool itIsANull = object.IsNull(obj); is incorrect because object is type and not an instance. 是不正确的,因为对象是类型而不是实例。 Change it to : 将其更改为:

bool itIsANull = (new object()).IsNull();

Or you can call it on class MyExtClass but not on object class (which is located in mscore.lib) : 或者您可以在类MyExtClass上调用它,但不能在对象类(位于mscore.lib中)上调用它:

MyExtClass.IsNull(new object());

PS It looks like you missed something about extension methods. PS看起来你错过了一些关于扩展方法的东西。 The truth is that they have nothing to do with classes that they extend. 事实是,他们与他们扩展的类无关。 It's just a convenience that is provided for us by Intellisense with use of reflection. 这只是Intellisense使用反射为我们提供的便利。

Object class is located in mscorelib and is immutable. 对象类位于mscorelib中,并且是不可变的。 You can't add something to it. 你不能添加东西。 But what really happens is that Intellisense searches for all public methods that are located in public static classes and accept first argument with keyword 'this' as parameter. 但真正发生的是Intellisense搜索位于公共静态类中的所有公共方法,并接受带有关键字“this”作为参数的第一个参数。 If one is found it's 'mapped' to the class that it extends. 如果找到一个,它会'映射'到它扩展的类。 So when we type obj.MyExtMethod() on instance of that class it is automatically converted by compiler to Helper.MyExtMethod(obj); 因此,当我们在该类的实例上键入obj.MyExtMethod()时,它会被编译器自动转换为Helper.MyExtMethod(obj); (if helper is our static class); (如果帮助者是我们的静态类);

You wrote an extension method, and extension methods exist in a different type but extend objects of the specified type by another method. 您编写了一个扩展方法,扩展方法以不同的类型存在,但是通过另一种方法扩展指定类型的对象。

But when you call object.IsNull() , then you are looking for a static method that exists on the object type. 但是当你调用object.IsNull() ,你正在寻找一个存在于对象类型上的静态方法

Instead, you have two ways to call your method: 相反,您有两种方法来调用您的方法:

// either the static method on the class
MyExtClass.IsNull(obj);

// or using the actual feature of extension methods
obj.isNull();

Because it's an extension method, the latter form will be automatically converted into the former at compile time. 因为它是一个扩展方法,后一种形式将在编译时自动转换为前者。

You are calling the extension method on the object itself. 您正在调用对象本身的扩展方法。 You should call the methd on the instance instead - 你应该在实例上调用methd -

bool itIsANull = obj.IsNull()

Try: 尝试:

    class Program
    {
        static void Main(string[] args)
        {
            var o = new object();
            if (o.IsNull())
            {
                Console.Write("null");
            }
        }
    }

    public static class Request
    {
        public static bool IsNull(this object obj)
        {
            return ReferenceEquals(obj, null);
        }
    }
public static class MyExtClass
    {
        // the "IsNull" extension to "object"  
        public static bool IsNull(this object obj)
        {
            return object.ReferenceEquals(obj, null);
        }

    }

    public class SomeOtherClass
    {
        public static void TryUsingTheExtension()
        {
            object obj =null;

            bool itIsANull = obj.IsNull();
        }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM