You cannot use the explicit and implicit operators for this in the documentation. Can this be beautifully implemented? Or just a method / constructor to do?

Briefly about the situation: the first dll describes the base class (Base) and all sorts of methods that return an object of the base class (public Base Foo ()). The second dll uses the first dll, it describes a class that is inherited at the base (Child: Base) and methods that accept a descendant (public void Bar (Child c)). As a result, we want to do so Bar (Foo ()).

  • 2
    Uh, maybe you just need a caste? Is your object really derived type, or basic? Show the code. - VladD
  • @VladD, objects of a really different type, tried to explain this in the text of the question. - Julidae
  • one
    Bar(Foo()) - this should not be done, and the compiler will not. Options: Bar((Child)Foo()) - an exception in the case when the result of Foo not Child ; Bar(Foo() as Child) - need null check inside Bar . - Igor

2 answers 2

Ok, since these are really different types, the question arises whether it is possible to build an instance of a derived from an instance of the base type.

If not, then the task, of course, has no solution.

If possible, the easiest way is probably to do it with the extension method:

 // первая DLL class Point2D { public double X { get; set; } public double Y { get; set; } } class First { public static Point2D Foo() => new Point2D() { X = 1, Y = 2 }; } 
 // вторая DLL class Point3D : Point2D { public double Z { get; set; } } static class PointExtensions { public static Point3D LiftTo3D(this Point2D self) => new Point3D() { X = self.X, Y = self.Y, Z = 0 }; } class Program { public static void Bar(Point3D p) { /* ... */ } public static void Main() { Bar(First.Foo().LiftTo3D()); } } 

Note that in this case you will work not with the object that is returned from the first DLL, but with a completely outsider. So changes in this foreign object will not be reflected in the original object. If this does not suit you, then the derived class (in the example Point3D ) will have to aggregate the base Point2D ( Point2D ). (Yes, it is expensive and inconvenient, but if this is needed, do you really need inheritance?)

  • I really doubt about the uselessness of inheritance in this case. - Julidae
  • @Julidae: Well, this is what you are looking at for your task. My job is to ask and warn :) - VladD

Use UpCast and DownCast.

First you need to create an instance of the derived class. Then bring it to the base class - and if there is a need for it back you can lead to the derived class. Why it is used can be read on the forum . Just like that, it is impossible to bring a derivative from a base type instance (as far as I know)

  • That's the problem, that the base class instance comes to me from the connected dll, and I create the derivative one. - Julidae
  • and what prevents from creating an instance of a class that inherits the base class? I can not understand the problem. include the library into the build and plug in the nemespace using using - Volodymyr
  • one
    @Volodymyr: The problem, obviously, is that an object is created in an uncontrolled assembly that does not know about the child object. - VladD