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?)
Bar(Foo())- this should not be done, and the compiler will not. Options:Bar((Child)Foo())- an exception in the case when the result ofFoonotChild;Bar(Foo() as Child)- neednullcheck insideBar. - Igor