Ambiguous extension method

前端 未结 4 1082
太阳男子
太阳男子 2020-12-28 13:13

I am making the following call to an extension method:

database.ExecuteScalar(command).NoNull(string.Empty);

I get an error t

相关标签:
4条回答
  • 2020-12-28 13:19
    1. Remove the ambiguity by redefining or eliminating one of the methods at the source. You don't need redundancy.
    2. If you do not control the source, include only one of them in your class file via the using directive.
    3. If you still need both namespaces in the given class file, invoke the version you wish simply as a static class call, unambiguously identifying the method via a fully qualified class name.

    -

     Abc.Xyz.ExtensionsClass.NoNull(database.ExecuteScalar(), string.Empty);
    
    0 讨论(0)
  • 2020-12-28 13:22

    You should change the signature of one (or both of them) to differentiate what it does. This seems like duplication of code somewhere unless these do different things. Though if they do different things I would think you would differentiate that in the names. I'd recommend creating some sort of enumeration (a flag maybe) to pass as an extra argument to one of the methods.

    0 讨论(0)
  • 2020-12-28 13:29

    Just in case somebody will need this...

    Ambiguity can be resolved if concurrent namespaces which have extension methods with same name, are included at different levels (most inner included namespace will have priority).

    For example:

    using Namespace1;
    namespace MyApplication 
    {
        using Namespace2;
        ...
        db.Execute(); // Namespace2 Execute() will be called
    }
    
    0 讨论(0)
  • 2020-12-28 13:46

    I would strongly suggest that you rename one of the extension methods. Depending on what else you do, you could possibly just remove the using directive for one of those namespaces, but that won't help if you need both namespaces for other things. (This leads to a suggestion to put extension methods in their own namespace, of course.) Renaming is likely to simplify things in general though.

    0 讨论(0)
提交回复
热议问题