How to get the used interface on an object

G

Guest

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; }
}
}
}
 
J

Joanna Carter [TeamB]

"Niklas" <[email protected]> a écrit dans le message de (e-mail address removed)...

| How do I get the currently used interface on an object? See code below for
| an example.

| 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?
| }
| }

You can't do this.

Interfaces are not objects and they do not possess a GetType() method. An
interface doesn't have anywhere to put code, it is simply a contract that an
instance of a class can fulfil.

You could always change your WriteClass code to take a Type parameter.

private static void WriteType(Type type)
{
Console.WriteLine(type.Name);
}

....and then call it like this :

{
MyClass2 y = new MyClass2();
WriteType(y.GetType());
WriteType(typeof(IMyInterface2));
WriteType(typeof(IMyInterface1));
}

But then you might as well not bother with a special method, just call
Type.Name;

{
Console.WriteLine(typeof(MyClass).Name);
Console.WriteLine(typeof(IMyInterface2).Name);
Console.WriteLine(typeof(IMyInterface1).Name);
}

Joanna
 
G

Guest

Hi
Thank you. I thought so as well but I just wanted to make sure that I hadn't
missed anything. I the end I wanted to do different actions depending what
interface the caller used at the moment on the object. I have posted a
resulted question regarding how to enforce that a class only inherit one and
only one interface.
Regards
/Niklas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top