Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extend an existing interface

Tags:

c#

interface

dll

I have encountered a problem. I am using an external library in my program that provides an interface, IStreamable (I don't have the sources of this interface).

I then implement the interface in a DLL that I created, DFKCamera class.

In my current program (which unfortunately I can't fully modify because I am just writing a plugin for it) I then only have access to those methods of DFKCamera that are defined in the interface IStreamable. However, I need to access a different method I have written in DFKCamera for my plugin to work (a method that the rest of the program doesn't use and as such isn't defined in IStreamable).

Is it possible to extend the definition of an interface in C#? If I could extend the IStreamable interface, then I'd have access to the new method.

As is, this is the situation:

//In ProgramUtils.DLL, the IStreamable interface is defined
//I have only the .DLL file available
namespace ProgramUtils {
    public interface IStreamable {
       //some methods
    }
}

//In my DFKCamera.DLL
using ProgramUtils;

class DFKCamera: IStreamable {
    //the IStreamable implementation code
    ....
    //the new method I wish to add
    public void newMethod() {}


//In the the program that uses DFKCamera.DLL plugin
//The program stores plugin Camera objects as IStreamable DLLObject;
IStreamable DLLObject = new DFKCamera();
//This means that I cannot access the new method by:
DLLObject.newMethod(); //this doesn't work!

Is there a way to extend the IStreamamble interface with the newMethod declaration even though I don't have access to the sources for the IStreamable interface?

I know it's possible to use partial interface definitions to define an interface across files, however that only works if the partial keyword is used across both files and if these are compiled in a single .DLL

I hope this is clear enough!

like image 474
Carlo M. Avatar asked Mar 19 '14 00:03

Carlo M.


People also ask

How do you extend an interface?

An interface can extend another interface in the same way that a class can extend another class. The extends keyword is used to extend an interface, and the child interface inherits the methods of the parent interface.

Can we Extending interface?

Yes, we can do it. An interface can extend multiple interfaces in Java.

What happens when you extend an interface?

An interface is a contract without implementations (Java8 introduced default methods). By extending you extend the contract with new "names" to be implemented by concrete class.

Can you extend an interface in TypeScript?

In TypeScript, interfaces can also extend classes, but only in a way that involves inheritance. When an interface extends a class, the interface includes all class members (public and private), but without the class' implementations.


2 Answers

You could use an extension method:

public static class IStreamableExtensions
{
    public static void NewMethod(this IStreamable streamable)
    {
        // Do something with streamable.
    }
}
like image 196
Andrew Whitaker Avatar answered Nov 07 '22 19:11

Andrew Whitaker


You can inherit from the interface with a custom interface:

public interface IDFKStreamable : IStreamable
{
    void NewMethod();
}

Then any object which implements the custom interface would also have to implement IStreamable and you can just use the custom interface in your code:

public class DFKCamera : IDFKStreamable
{
    // IStreamable methods

    public void NewMethod() {}
}

// elsewhere...

IDFKStreamable DLLObject = new DFKCamera();
DLLObject.NewMethod();

Since it's still an IStreamable you should still be able to use it as one in existing code as well:

someOtherObject.SomeMethodWhichNeedsAnIStreamable(DLLObject);
like image 35
David Avatar answered Nov 07 '22 21:11

David