Why do extension methods not work with namespace aliasing?
Asked Answered
E

2

17

This may be an ignorant question, but I'm unsure why I can not use namespace aliasing and extension methods together.

The following example works just fine:

Program.cs

using System;
using ExtensionMethodTest.Domain;

namespace ExtensionMethodTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var m = new Domain.MyClass();
            var result = m.UpperCaseName();
        }
    }
}

MyClass.cs

using System;

namespace ExtensionMethodTest.Domain
{
    public class MyClass
    {
        public string Name { get; set; }
    }
}

MyClassExtensions.cs

using System;

namespace ExtensionMethodTest.Domain
{
    public static class MyClassExtensions
    {
        public static string UpperCaseName (this MyClass myClass)
        {
            return myClass.Name.ToUpper();
        }
    }
}

However, when I alias domain as follows in Program.cs:

using Domain = ExtensionMethodTest.Domain;

The extension method no longer works..

This can be rather frustrating when I'm dealing with converting various domain objects to contract objects (let's say I have 4 domain assemblies and 4 contract assemblies) for use in a web service. Using aliasing would be very handy as I could alias as follows and continue to use the various extension methods (such as ToContract, etc.):

using BillingContracts = Namespace.Billing.Contracts;
using IssuingContracts = Namespace.Issuing.Contracts;

etc...

I look forward to the answer.. I'm sure it's straight forward, but I, for the life of me, can't figure out why it doesn't work.

Thanks!

Empoverish answered 26/7, 2010 at 17:47 Comment(1)
It's a good question. I'm not sure of the exact answer, but it probably has to do with the fact that the extension method is tied to the extension method's type, not to its namespace. Hence, the type itself must be visible within your class, so you would still have to include a complete using statement.Quass
P
11

Make sure to still add a non-aliased using statement:

Program.cs

using System;
using ExtensionMethodTest.Domain; //DON'T FORGET A NON-ALIASED USING
using MyDomain = ExtensionMethodTest.Domain;

namespace ExtensionMethodTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var m = new MyDomain.MyClass();
            var result = m.UpperCaseName();
        }
    }
}

MyClass.cs

using System;

namespace ExtensionMethodTest.Domain
{
    public class MyClass
    {
        public string Name { get; set; }
    }
}

MyClassExtensions.cs

using System;

namespace ExtensionMethodTest.Domain
{
    public static class MyClassExtensions
    {
        public static string UpperCaseName (this MyClass myClass)
        {
            return myClass.Name.ToUpper();
        }
    }
}
Psycho answered 26/7, 2010 at 17:55 Comment(4)
I was aware that a non aliased using statement would work, but I'm just curious as to why an aliased using statement doesn't work. :)Empoverish
An aliased using statement does not bring the aliased namespace into scope for name resolution. All it does is provide an alias to the specific namespace for convenience. Even though both of these statements are "using" statements, they act quite differently. It might help to think of "using X;" as a namespace #include, and "using Foo = X;" as a namespace #define.Boo
Ah, in that case I'd venture to guess that it's because defining an alias is not actually giving you access to that namespace, but providing a shortcut name to it... It's the same reason you would have to do BillingContracts.MyClass() instead of just MyClass() when you use an alias.Psycho
@JaredReisinger: Yep, you nailed it.Turboelectric
H
0

I also love to use namespace aliasing but its not working in case of Extension methods. So one thing that i did is, I changed the namespace of extension class to same namespace that my main project has (although my extension class resides in sub folder of main project).

Suppose I have a project myFirstProj which surely has namespace myFirstProj for root classes. My extension class is present in myFirstProj/Common/myExtensionClass with contains namespace myFirstProj.Common { //myExtensionClass }.

So now what I did is, I changed the namespace of myExtensionClass from namespace myFirstProj.Common{ //myExtensionClass } to namespace myFirstProj{ //myExtensionClass } .

Now i can use my extension methods in my whole project myFirstProj event without specifying using statement for my extension class.

I know this isn't a standard way to that but I haven't found any other workaround for it expect this one because for my Project there is a requirement to go with namespace aliasing for project namespaces.

Hammad answered 25/5, 2017 at 6:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.