sherbang
sherbang

Reputation: 16215

C# subclass while maintaining name. Deep voodoo?

I have a dll that I'm working with, it contains a class foo.Launch. I want to create another dll that subclasses Launch. The problem is that the class name must be identical. This is used as a plugin into another piece of software and the foo.Launch class is what it looks foe to launch the plugin.

I've tried:

namespace foo
{
    public class Launch : global::foo.Launch
    {
    }
}

and

using otherfoo = foo;
namespace foo
{
    public class Launch : otherfoo.Launch
    {
    }
}

I've also tried specifying an alias in the reference properties and using that alias in my code instead of global, that also didn't work.

Neither of those methods work. Is there a way I can specify the name of the dll to look in within the using statement?

Upvotes: 4

Views: 355

Answers (4)

Hamlet Hakobyan
Hamlet Hakobyan

Reputation: 33381

Maybe you need to use extern aliases.

For example:

//in file foolaunch.cs

using System;

namespace Foo
{
    public class Launch
    {
        protected void Method1()
        {
            Console.WriteLine("Hello from Foo.Launch.Method1");
        }
    }
}

// csc /target:library /out:FooLaunch.dll foolaunch.cs

//now subclassing foo.Launch

//in file subfoolaunch.cs

namespace Foo
{
    extern alias F1;
    public class Launch : F1.Foo.Launch
    {
        public void Method3()
        {
            Method1();
        }
    }
}


// csc /target:library /r:F1=foolaunch.dll /out:SubFooLaunch.dll subfoolaunch.cs

// using
// in file program.cs

namespace ConsoleApplication
{
    extern alias F2;
    class Program
    {
        static void Main(string[] args)
        {
            var launch = new F2.Foo.Launch();
            launch.Method3();
        }
    }
}

// csc /r:FooLaunch.dll /r:F2=SubFooLaunch.dll program.cs

Upvotes: 0

Sebastian Piu
Sebastian Piu

Reputation: 8008

as Chris said, you can use an alias on your original assembly.

If you can't you that, then you might be able to cheat by using a 3rd assembly

Assembly1.dll (your original)

namespace foo { 
     public class Launch {}
}

Assembly2.dll (dummy)

namespace othernamespace { 
     public abstract class Dummy: foo.Launch {}
}

Assembly3.dll (your plugin)

namespace foo{ 
     public class Launch: othernamespace.Dummy{}
}

I'm not even proud of this!

Upvotes: 2

Chris Hannon
Chris Hannon

Reputation: 4134

You'll need to alias the original assembly and use an extern alias to reference the original assembly within the new one. Here's an example of the use of the alias.

extern alias LauncherOriginal;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace foo
{
    public class Launcher : LauncherOriginal.foo.Launcher
    {
        ...
    }
}

Here's a walkthrough that explains how to implement that.

Also, you'd mentioned that you tried to use an alias before and encountered problems but you didn't say what they were, so if this won't work then please mention what went wrong.

Upvotes: 6

BoeroBoy
BoeroBoy

Reputation: 1210

Class name can be identical if it's defined in another namespace, but it boggles the mind why anybody would want to do that to themselves.

Upvotes: 0

Related Questions