1

Is there a way to retain the class inheritance in c# while at the same time obeying the interface inheritance rules of COM interop? Or in a different sense, how can one duplicate the functionality of AutoDual with explicit interfaces?

I've received an API library written in C# from another company that uses Microsoft.Runtime.InteropServices. I am using it to compile to a DLL which our legacy software (in powerbuilder) uses to call the API. I'm trying to use ClassInterfaceType.None to avoid ClassInterfaceType.AutoDual which may break the functionality in the future. I am running into the following issue:

The company who wrote the API library used good Object Oriented design. While this reduces redundancy in the c# code, it works against my purposes because of the class inheritance structure going against COM interface requirements. E.g. the Get methods for each request (there are many) call the same method which expects an inherited generic type:

Generic Method:

private T Get<T>(string Foo, string Bar) where T : UmbrellaResponse, new() { /* get the response*/ }

Two of many specific methods:

namespace SeparateFromTheObjects
{
    public SpecificResponse1 GetSpecificResponse1(string token)
        {
            return Get<SpecificResponse1>(token, "specific_thing");
        }

    public SpecificResponse2 GetSpecificResponse2(string token)
        {
            return Get<SpecificResponse2>(token, "specific_thing");
        }
}

Which currently inherits like this: public class SpecificResponse : UmbrellaResponse

So if I create an interface with all needed properties/methods and change it to SpecificResponse : ISpecificResponse or even : IUmbrellaResponse, all the methods can't be used because they no longer inherit from the CLASS UmbrellaResponse.

Basically, is there a way to retain the class inheritance in c# while at the same time obeying the interface inheritance rules of COM interop? Or in a different sense, how can one duplicate the functionality of AutoDual with explicit interfaces?

If impossible, how can you write methods (such as the get method above) to accommodate multiple objects that do not inherit from the same type?

I have read the following, but do not believe it contains an answer: Why should I not use AutoDual?

Community
  • 1
  • 1
smurtagh
  • 1,187
  • 9
  • 17
  • Several misconceptions and it would take writing a book to get them out of the way. One that won't make you happy, nobody is going to write it. But you are laser-focused on a problem you don't actually have. ClassInterfaceType.AutoDual is tricky because it exposes too many details. The kind that requires client code to be recompiled when the C# class changes. Normally a big problem, but *not* in your case. You control the client code and have no problem knowing when to recompile it. – Hans Passant Apr 21 '17 at 13:40
  • @Hans_Passant If I'm understanding correctly, you're saying since I'm the only one compiling, I shouldn't run into any issues with AutoDual? I guess I've misunderstood what is meant in the post I linked when Kevin quotes an MVP, "For early binding you'd have to rebuild the clients each time the interface was regenerated." I don't know what type of client would early bind (sorry for my ignorance). – smurtagh Apr 21 '17 at 14:07
  • It is one of the misconceptions, InterfaceType.AutoDual is not a problem. The issue is that it only works on an interface and you don't have any, so you have to limp along with ClassInterfaceType.AutoDual. Which is very convenient, it auto-generates the interface from the class definition. But dangerous since you can't easily tell that the interface changed and became incompatible. Or avoid it. Likely to crash and burn client code that uses the old interface definition, getting it recompiled is a big hassle since somebody else has to do it. Not your problem, you have control over both. – Hans Passant Apr 21 '17 at 14:59
  • Maybe some overly strong wording here then: [link](https://msdn.microsoft.com/en-us/library/ms182205.aspx). Thanks @Hans_Passant. – smurtagh Apr 21 '17 at 17:46
  • @smurtagh I agree – StayOnTarget Jan 31 '19 at 17:04

0 Answers0