From ff628377aa277ac5ab4ff6c5fee76d7457ac8485 Mon Sep 17 00:00:00 2001 From: KrotkikhFV Date: Tue, 27 Sep 2022 11:08:27 +0300 Subject: [PATCH] polymorphism with interface doesnt work If we are using derived class that implements an interface throught base type or that interface, then polymorphism doesnt work. It works only in case using derived type explicitly. "public new" it is not enough. The way to go is to explicitly implement the part of the interface that you want to override. https://stackoverflow.com/questions/12314990/override-method-implementation-declared-in-an-interface --- nng.NET/Factory.cs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/nng.NET/Factory.cs b/nng.NET/Factory.cs index c9d4a07..7ba96d3 100644 --- a/nng.NET/Factory.cs +++ b/nng.NET/Factory.cs @@ -116,17 +116,25 @@ public NngResult StreamFrom(INngAio aio) namespace Compat { - public class Factory : FactoryBase + public class Factory : FactoryBase, ISocketFactory { - public new NngResult PairOpen() => Pair0Socket.Open(); + public new NngResult PairOpen() => Pair0Socket.Open(); + NngResult ISocketFactory.PairOpen() + { + return this.PairOpen(); + } } } - + namespace Latest { - public class Factory : FactoryBase + public class Factory : FactoryBase, ISocketFactory { - public new NngResult PairOpen() => Pair1Socket.Open(); + public new NngResult PairOpen() => Pair1Socket.Open(); + NngResult ISocketFactory.PairOpen() + { + return this.PairOpen(); + } } } }