Subclass of C # when saving a name. Deep voodoo?

I have a dll I'm working with, it contains the class foo.Launch. I want to create another DLL that runs subclasses of Launch. The problem is that the class name must be identical. This is used as a plugin in another piece of software, and the foo.Launch class is what seems nasty to run the plugin.

I tried:

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

and

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

I also tried to specify an alias in the reference properties and use this alias in my code instead of the global one, which also did not work.

None of these methods work. Is there a way to specify the name of the DLL to search inside the using statement?

+4
source share
4 answers

You will need the alias of the original assembly and use extern alias to reference the original assembly in the new one. Here is an example of using an 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 this.

In addition, you mentioned that you tried to use the alias before and encountered problems, but you did not say that they were, so if this does not work, please indicate what went wrong.

+6
source

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

If you cannot do this, then you can cheat using the 3rd build

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 it!

+2
source

A class name can be identical if it is defined in a different namespace, but it scares the mind why someone would like to do this for themselves.

0
source

You might need to use external aliases.

For instance:

 //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 
0
source

Source: https://habr.com/ru/post/1415601/


All Articles