473,405 Members | 2,294 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 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 1472
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Serge Calderara | last post by:
Dear all, I read many documents and article based on Interface, but I am still blind with that and start to have no more hairs. I try to clearly understand when should I create Interface ? ...
4
by: Doug | last post by:
I am working on an existing .NET (C Sharp) component that had a com interface that was used by a VB component. When it was originally written, it had the SSEAssemblyCom class below - minus the two...
10
by: Bob | last post by:
This has been bugging me for a while now. GetType isn't availble for variables decalred as interface types, I have to DirectCast(somevariable, Object). In example: Sub SomeSub(ByVal...
0
by: Eric | last post by:
I'm trying to create a single interafce that can be implelment by either a ..Net class or a COM object. Here's what I'm tried to do. I defined an interface using VB.net in an assembly all by...
12
by: masoud bayan | last post by:
I've come across something in Interface implementation that I am not sure is correct behavior in VB.NET (and maybe C#) or not? Consider following example: Public Interface IShape
9
by: Kiran | last post by:
Hi, As a convention we always create interfaces before creating classes for class libraries. we then implement this interface in a class. Can someone point out the reasons\advantages for...
15
by: Gustaf | last post by:
Using VS 2005. I got an 'IpForm' class and an 'IpFormCollection' class, containing IpForm objects. To iterate through IpFrom objects with foreach, the class is implemented as such: public class...
6
by: JDT | last post by:
Hi, Can we pass a member function in a class as a callback function? Someone instucted me that I can only use a static functon or a global function as a callback. Your help is appreciated. JD
6
by: S_K | last post by:
Hi, I've been toying around with interfaces in C#. They are fun but can anybody give me some examples of where interfaces are used and what they are used for? Thanks so much. Steve
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.