YWE
YWE

Reputation: 2909

C# public type alias?

What I want to do is write some classes in C# in a new namespace that act as wrapper classes for classes in another namespace. Sometimes a wrapper class is not needed but I still want a corresponding class in the new namespace. And I want an exact copy of the class. Is there a way to define the class in the new namespace by referring to the definition of another class? In other words I want an alias.

To clarify what I mean, if the existing namespace is named "Namespace1" and the new namespace is named "Namespace2", using code like this in Namespace2:

using Class1 = Namespace1.Class1;

would not work because Namespace2.Class1 would not exist. Class1 would only be aliased "private" to Namespace2 and not "public" to Namespace2. If I could use Namepsace2.Class1 from outside the namespace, and if that would still refer to Namespace1.Class1, then that would be what I want.

I figured there might be a way to accomplish this with attributes or reflection maybe. If there were some pre-processor directives or macros that could copy code that would work too, but obviously C# doesn't have anything like that.

Upvotes: 11

Views: 8493

Answers (7)

Serg Kryvonos
Serg Kryvonos

Reputation: 4677

You could inherit the class:

namespace N2
{
   public class Class1 : N1.Class1
   { }
}

Upvotes: 2

vgru
vgru

Reputation: 51214

Not sure what you are trying to accomplish, but take a look at AutoMapper also.

Upvotes: 0

Jeremy McGee
Jeremy McGee

Reputation: 25200

It sounds like you need to map one class definition to another.

This can be done manually (through lots of boilerplate code) or automatically through a tool like AutoMapper.

Upvotes: 3

Ariel
Ariel

Reputation: 5830

WRT wrapper classes, I find many times that people wrap classes only to extend functionality, instead of using that to express an is-a relationship. For those cases, you may consider Extension Methods.

This allows you to write methods for existing classes instead of deriving just to add some more functionality. For ex.

/// <summary>
/// ICollectionExtensions
/// </summary>
internal static class ICollectionExtensions
{
    public static void AddNew<T>(this ICollection<T> self, T data)
    {
        if (self != null && !self.Contains(data))
        {
            self.Add(data);
        }
    }
}

Then, you can .AddNew() on ICollection<T> types.

Upvotes: 0

danswain
danswain

Reputation: 4177

Yes you can do this if I understand you correctly here the msdn article on alias namespacing

here's the magic

using colAlias = System.Collections

so I'm aliasing System.Collections under my own namespace.

here's the example

using colAlias = System.Collections;
namespace System
{
    class TestClass
    {
        static void Main()
        {
            // Searching the alias:
            colAlias::Hashtable test = new colAlias::Hashtable();
        // Add items to the table.
        test.Add("A", "1");
        test.Add("B", "2");
        test.Add("C", "3");

        foreach (string name in test.Keys)
        {
            // Seaching the gloabal namespace:
            global::System.Console.WriteLine(name + " " + test[name]);
        }
    }
}

}

Upvotes: 0

Reed Copsey
Reed Copsey

Reputation: 564413

If you define the class in a different namespace, it will be a different class. MyNewNamespace.MyClass and MyOldNamespace.MyClass are two distinct types in .NET.

You can easily encapsulate all of your first class's public API in the new class, but this will require some boilerplate code. AOP may provide a way to do this more simply, using something like PostSharp (with a custom filter).

Upvotes: 2

Michael La Voie
Michael La Voie

Reputation: 27926

You can use Using to create an alias to a type:

using Project = PC.MyCompany.Project;

Upvotes: 5

Related Questions