471,319 Members | 1,562 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

How to get the used interface on an object

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

Feb 22 '06 #1
2 1399
"Niklas" <Ni****@discussions.microsoft.com> a écrit dans le message de news:
81**********************************@microsoft.com...

| 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

--
Joanna Carter [TeamB]
Consultant Software Engineer
Feb 22 '06 #2
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

"Joanna Carter [TeamB]" wrote:
"Niklas" <Ni****@discussions.microsoft.com> a écrit dans le message de news:
81**********************************@microsoft.com...

| 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

--
Joanna Carter [TeamB]
Consultant Software Engineer

Feb 22 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Serge Calderara | last post: by
4 posts views Thread by Doug | last post: by
12 posts views Thread by masoud bayan | last post: by
15 posts views Thread by Gustaf | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.