Here at work we mainly used Delphi, but also some C#.
So today a college asked me an interesting question today: How do I perform Intreface mapping in C#? And since I do not know here is the question for you:
First the Delphi Example:
unit UnitInterfaceMapping;
interface
type
IMyFirstInterface = interface(IInterface)
procedure DoSomethingInteresting;
procedure DoSomethingElse;
end;
IMyNextInterface = interface(IInterface)
procedure DoSomethingInteresting;
end;
TMyCombinedObject = class(TInterfacedObject, IMyFirstInterface, IMyNextInterface)
private
procedure IMyFirstInterface.DoSomethingInteresting = DoSomethingInterestingFirst;
procedure IMyNextInterface.DoSomethingInteresting = DoSomethingInterestingNext;
public
procedure DoSomethingInterestingFirst;
procedure DoSomethingInterestingNext;
procedure DoSomethingElse;
end;
implementation
uses
VCL.Dialogs;
{ TMyCombinedObject }
procedure TMyCombinedObject.DoSomethingElse;
begin
ShowMessage('DoSomethingElse');
end;
procedure TMyCombinedObject.DoSomethingInterestingFirst;
begin
ShowMessage('DoSomethingInterestingFirst');
end;
procedure TMyCombinedObject.DoSomethingInterestingNext;
begin
ShowMessage('DoSomethingInterestingNext');
end;
end.
Form a Form I call my code:
uses
UnitInterfaceMapping;
procedure TForm14.Button1Click(Sender: TObject);
var
MyFirstInterface: IMyFirstInterface;
MyNextInterface: IMyNextInterface;
begin
MyFirstInterface := TMyCombinedObject.Create;
MyFirstInterface.DoSomethingInteresting;
MyFirstInterface.DoSomethingElse;
MyNextInterface := TMyCombinedObject.Create;
MyNextInterface.DoSomethingInteresting;
end;
The result is three dialog boxes, one from each method.
Then I tried to port it to C#:
using System;
namespace InterfaceMapping
{
public interface IMyFirstInterface
{
void DoSomethingInteresting();
void DoSomethingElse();
}
public interface IMyNextInterface
{
void DoSomethingInteresting();
}
public class CombinedObject : IMyFirstInterface, IMyNextInterface
{
public void DoSomethingInteresting()
{
throw new NotImplementedException();
}
void IMyFirstInterface.DoSomethingElse()
{
throw new NotImplementedException();
}
void IMyFirstInterface.DoSomethingInteresting()
{
throw new NotImplementedException();
}
}
class Program
{
static void Main(string[] args)
{
new CombinedObject() <== Problem
}
}
}
The main point is when creating a new instance of CombinedObject
I only see the DoSomethingInteresting
method.
So in short: How do I perform Interface mapping in C#