简体   繁体   中英

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?

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.

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!

如果类名在另一个名称空间中定义,则它可以是相同的,但它让人难以理解为什么有人想要自己这样做。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM