Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do extension methods not work with namespace aliasing?

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!

like image 234
Ian P Avatar asked Jul 26 '10 17:07

Ian P


People also ask

What namespace can you use when you write an extension method?

Extension methods are brought into scope at the namespace level. For example, if you have multiple static classes that contain extension methods in a single namespace named Extensions , they'll all be brought into scope by the using Extensions; directive.

Can we create alias of a namespace?

You can also create an alias for a namespace or a type with a using alias directive.

What is alias in namespace?

namespace alias definition Namespace aliases allow the programmer to define an alternate name for a namespace. They are commonly used as a convenient shortcut for long or deeply-nested namespaces.

How do you call an extension method?

To define and call the extension methodDefine a static class to contain the extension method. The class must be visible to client code. For more information about accessibility rules, see Access Modifiers. Implement the extension method as a static method with at least the same visibility as the containing class.


1 Answers

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();
        }
    }
}
like image 148
myermian Avatar answered Oct 22 '22 22:10

myermian