简体   繁体   中英

Inherit a class with same namespace and same class name

I manage to work it out in 2 Classes, but I failed in 3. Let's say I have an assembly A.DLL which have namespace = "City" and a class= "NY"

(I will refer this class in my question as "A")

In My new project My namespace is = "City" and I have a class = "NY" (I will refer this class in my question as "B")

So to implement B : A (B Inherit/Extend from A) I created an alies to the reference A.DLL and in "B" class refere with "extern alias oldDll"

namespace City
{
    extern alias oldDll;

    public class NY : oldDll::City.NY
    {
        public NY() : base()
        {
        }
    }
}

This works fine! :)

The problem is when I try to use the new assebmly (B.DLL / "B" class) in another assembly with have a reference only to B.DLL (I will refer this class in my question as "C") and create a new intance of "City.NY" I get an exception, I think it's because "A.DLL" is not loading.

How can I load it (if this is the problem) from B? I don't have access the "C.EXE" or C codes (He is only creating City.NY from B.DLL). *Note: I cant re compile C. I can only run it.

So I have a class "C" which create a class "B" which inherits from "A"

I thought of manualy load "A.DLL" assembly in "B" code, but the first thing that happens is the base() constructor.. :\\

Thanks for helping

Correct me anyone because I'm not 100% sure but usually you have to reference both assemblies to get it working(that's way some packages have dependencies, which also have to be downloaded and referenced in project).

I think it won't compile either without missing assembly.

The first thing that happens is actually not the base constructor but the type constructor. You can specify this with the following:

static B()
{
}

Note that access modifiers are not allowed on type constructors. But either way, I guess the base type is loaded even before that, so you don't have a chance here.

Also configuration files won't help. They can redirect existing references (see https://msdn.microsoft.com/en-us/library/7wd6ex19%28v=vs.110%29.aspx ), but I don't think they can add references that were not there before.

You can't.

Simply stated - having class City.NY in B.dll inherit from City.NY in A.dll is a breaking change that would require the recompilation of assemblies that depend on B.dll .

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