views:

64

answers:

1

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!

+2  A: 

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();
        }
    }
}
myermian
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. :)
Ian P
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.
JaredReisinger
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.
myermian
@JaredReisinger: Yep, you nailed it.
Eric Lippert