Avatar of Skale

asked on 

How to return inherited class types from function?

I'm having a problem (in fact it is not an issue I can direct cast or safe cast) about one structure.

I have classes like below: All Class1 to Class5 inherited from ClassA and I have one function which is not created by myself returns type ClassA. Now I wonder is there any way to return any of the inherited (Class1,..., Class5) type desired class?

This is the function that cannot be modified because it comes from the COM interface

public ClassA GetMyObject(){...}

Open in new window

Now I would like to put in a function that I create and return my desired type is it possible? Or do you suggest anything different?

public <T> GetMyObjectCustom(<T> DesiredReturnFormat){(<T>) GetMyObject()}

Open in new window

.NET ProgrammingC#

Avatar of undefined
Last Comment

8/22/2022 - Mon