lunedì 6 luglio 2009

How To: implement multi Interface

In questo esempio decisamente interesante sono riportate 2 interfaccie e una classe che le implementa entrambe.

Nel caso d'uso prima di eseguire un metodo, ci si accerta che la classe implementi realmente l'interfaccia, tramite il tipo.

public interface interface1
{
void methodInterface1(string p1);
}

public interface interface2
{
void methodInterface2(string p1);
}


public class InterfacesTest : interface1, interface2
{

public void methodInterface1(string p1)
{
Console.WriteLine(p1);
}

public void methodInterface2(string p1)
{
Console.WriteLine(p1);
}
}

public class testUse
{
private InterfacesTest test = new InterfacesTest();


public void DoTest()
{
if (test.GetType().GetInterface("interface1") != null)
{
test.methodInterface1("A");
}

if (test.GetType().GetInterface("interface2") != null)
{
test.methodInterface2("B");
}

}
}

Nessun commento: