Hi
How do I get the currently used interface on an object? See code below for
an example.
Regards
/Niklas
using System;
namespace TestInterfaces
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
MyClass2 y = new MyClass2();
WriteClass(y); //Returns MyClass2
IMyInterface2 iy2 = y as IMyInterface2;
WriteInterface(iy2); //Here I woud like the function to write IMyInterface2
IMyInterface1 iy1 = y as IMyInterface1;
WriteInterface(iy1); //Here I woudlike the function to write IMyInterface1
Console.ReadLine();
}
private static void WriteClass(object obj)
{
Console.WriteLine(obj.GetType().Name);
}
private static void WriteInterface(object obj)
{
//What to do here?
}
}
public interface IMyInterface1
{
int MyInt1{ get; }
}
public interface IMyInterface2
{
int MyInt1{ get;}
int MyInt2{ get;}
}
public class MyClass2 : IMyInterface1, IMyInterface2
{
private int _MyInt1 = 4;
private int _MyInt2 = 5;
public int MyInt1
{
get { return _MyInt1; }
}
public int MyInt2
{
get { return _MyInt2; }
}
}
}