Interface delegation in C #

In Delphi, we can delegate an interface implementation to another class, I wonder if this is possible in C #?

For example, I have an IMyInterface interface, and I say that TMyClass implements IMyInterface. But actually it is not. Internally, I can use a keyword that implements the delegation of an interface implementation to another class without declaring each method in TMyClass.

+5
source share
5 answers

There is a simple answer: no, C # does not allow it in the same way as in Delphi.

, #, Delphi, : http://docwiki.embarcadero.com/RADStudio/en/Implementing_Interfaces, . " ( Win32 )".

, . # - ( ):

public class Blah : ISomeInterface
{
    public ISomeInterface implementer { getter and setter here }

    public int ISomeInterface.DoThis() 
    { 
        if (implementer) return implementer.DoThis(); 
    }
    public void ISomeInterface.DoThat(int param) 
    { 
        if (implementer) implementer.DoThat(param); 
    }

etc...

DoThis DoThat - ISomeInterface, Blah. # . Delphi (.. , ), implements class interface.

, implements delegation, #.

+5

, . , , , , . - , :

public interface ISomething
{
    void DoOne();
    void DoTwo();
}

public class Something : ISomething
{
    public virtual void DoOne() { }
    public virtual void DoTwo() { }
}

IMO, , , , .

, "" , ( ).

public interface ISomething
{
    void DoOne();
    void DoTwo();
}

public abstract class Something : ISomething
{
    public abstract void DoOne();
    public abstract void DoTwo();
}

, "-", - , . .

, , , . , .

0

:

// SomeThing.cs
public partial class SomeThing
{
   // your interface independant part
}

// SomeThing.ISomeThing.cs
public partial class SomeThing: ISomeThing
{
   // ISomeThing implementation
}

. .

0

, , .

Class1.cs

public partial class Class1: IInterface {}

Class1Impl.cs

public partial class Class1 { /*implementation */ }

, , . =) , , -.

0

# , (. ).

, .

MyClass.Specific.cs:

public partial class MyClass
{
    void MySpecificMethod() {...}
}

(MyClass.IMyInterface.cs) :

public partial class MyClass : IMyInterface
{
    void IMyInterface.MySpecificMethod() {...}
}

MyClass, .

0

All Articles