9

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.

YWE
  • 2,849
  • 28
  • 42

7 Answers7

6

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

using Project = PC.MyCompany.Project;
Michael La Voie
  • 27,772
  • 14
  • 72
  • 92
  • 1
    This is just a compiler trick, but doesn't actually "copy" the class in the other namespace. – Reed Copsey Jan 18 '10 at 20:07
  • @Reed Copsey - I see what you mean. I'm not sure if by "exact copy" he meant a real copy or just something that looks like a copy. @YWE - can you please clarify? – Michael La Voie Jan 18 '10 at 20:09
  • Yeah - it's unclear. I took it the other way, since he specified "public" in the question (which is meaningless for type aliasing) and because he made reference to "optional wrapper classes"... It sounds like he wants a public API that is a "copy" of another namespace's API. – Reed Copsey Jan 18 '10 at 20:17
  • et al. Sorry for being unclear. I edited my question to add some clarification, hopefully. I do need a "copy" of the class in the other namespace. And the kind of alias you suggested wouldn't work in my case. – YWE Jan 19 '10 at 14:19
3

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.

Jeremy McGee
  • 24,842
  • 10
  • 63
  • 95
2

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).

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
1

You could inherit the class:

namespace N2
{
   public class Class1 : N1.Class1
   { }
}
Sergei Krivonos
  • 4,217
  • 3
  • 39
  • 54
0

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]);
        }
    }
}

}

danswain
  • 4,171
  • 5
  • 37
  • 43
0

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.

Ariel
  • 5,752
  • 5
  • 49
  • 59
0

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

vgru
  • 49,838
  • 16
  • 120
  • 201