Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# subclass while maintaining name. Deep voodoo?

Tags:

c#

.net

c#-3.0

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?

like image 428
sherbang Avatar asked Feb 20 '23 07:02

sherbang


2 Answers

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.

like image 117
Chris Hannon Avatar answered Feb 22 '23 19:02

Chris Hannon


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!

like image 25
Sebastian Piu Avatar answered Feb 22 '23 19:02

Sebastian Piu