Daniel Suchan
Daniel Suchan

Reputation: 61

Define assembly for extension method

I'm trying to use extension method, but the method is defined twice with same name. Let's say A.Extensions.Ext() and B.Extensions.Ext() I need both references in my class and when trying

using A.Extensions;
using B.Extensions;
class C
{
    public void Main()
    {
        somevalue.Ext();
    }
}

I want to somehow define which method to use and I don't know how to do that. Thanks for help!

Upvotes: 0

Views: 420

Answers (1)

svoychik
svoychik

Reputation: 1327

There is a way to choose an extension method to be used

Assume you have two classes with extension methods for string

public static class Extension1
    {
        public static string GetLowerCaseResult(this string str)
        {
            return str.ToLowerInvariant();
        }
    }

    public static class Extension2
    {
        public static string GetLowerCaseResult(this string str)
        {
            return str.ToLowerInvariant();
        }
    }

to call it in Main method you have to explicitly specify class and method in this way

static void Main(string[] args)
        {
            var str = "QWERTY";
            Extension2.GetLowerCaseResult(str);
            Console.Read();
        }

Upvotes: 1

Related Questions