Code: Select all
internal class Program
{
interface IGetString
{
string GetString();
}
class A : IGetString
{
public string GetString() => "A";
}
class B : IGetString
{
public string GetString() => "B";
}
interface ICreate
{
T Create();
}
class C : ICreate, ICreate
{
A ICreate.Create()
{
return new A();
}
B ICreate.Create()
{
return new B();
}
}
static string X(ICreate creator)
{
return creator.Create().GetString();
}
static void Main()
{
ICreate createB = new C();
var s = X(createB);
Console.WriteLine("Result: " + s);
}
}
Aufrufe nur die Methode erstellen der ersten implementierten Schnittstelle: ICreate , icreate . Wenn Sie sie austauschen, wird das Ergebnis geändert.
Code: Select all
static string X(ICreate creator) where T : IToString
{
return creator.Create().GetString();
}