473,326 Members | 2,108 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,326 software developers and data experts.

Converion between interfaces

suppose I have an interface reference IA, to an object implementing let's
say, InterfaceA.

Is it possible to query IA and find out whether some other interface e.g.
IntefaceB is also supported?

Is it possible to get the list of all the interfaces supported from an
InterfaceRefernce and/or from an Object at runtime?

if above are possible then how to do it in C#?

TIA
Nov 16 '05 #1
3 1082
If you want to know if any interface is supported you can just use the C#
'is' operator, i.e.

Object o = // create somthing here

if(o is IMyInterface)
{
// whatever
}

Getting all interfaces is a matter of using reflection:

Type[] interfacesIImplement = o.GetType().GetInterfaces();

--
C#, .NET and Complex Adaptive Systems:
http://blogs.geekdojo.net/Richard
"Arjang" <Ar****************@NotTheRealPart.zorg> wrote in message
news:uc**************@TK2MSFTNGP14.phx.gbl...
suppose I have an interface reference IA, to an object implementing let's
say, InterfaceA.

Is it possible to query IA and find out whether some other interface e.g.
IntefaceB is also supported?

Is it possible to get the list of all the interfaces supported from an
InterfaceRefernce and/or from an Object at runtime?

if above are possible then how to do it in C#?

TIA

Nov 16 '05 #2
Thanks Richard,
but what i was really after, is that only by using an interface refernce
only. for example Let MyObj implement InrefaceA.

MyObj o= new MyObj();
IntrefaceA h;
h=(InterfaceA)o;

now if I pass h to a method only accepting InterfaceA, e.g.

public MethodA( IntrfaceA IntfA )
{
//need to find out wether object passes as intfA supports InterfaceB
if (IntfA is InterfaceB) //does this work??
{
// do something using interfaceB
}

}

In Delphi I can use QueryInterface to check and convert from one interface
to another.

"Richard A. Lowe" <ch*****@yumspamyumYahoo.com> wrote in message
news:eS**************@TK2MSFTNGP12.phx.gbl...
If you want to know if any interface is supported you can just use the C#
'is' operator, i.e.

Object o = // create somthing here

if(o is IMyInterface)
{
// whatever
}

Getting all interfaces is a matter of using reflection:

Type[] interfacesIImplement = o.GetType().GetInterfaces();

--
C#, .NET and Complex Adaptive Systems:
http://blogs.geekdojo.net/Richard
"Arjang" <Ar****************@NotTheRealPart.zorg> wrote in message
news:uc**************@TK2MSFTNGP14.phx.gbl...
suppose I have an interface reference IA, to an object implementing let's
say, InterfaceA.

Is it possible to query IA and find out whether some other interface e.g.
IntefaceB is also supported?

Is it possible to get the list of all the interfaces supported from an
InterfaceRefernce and/or from an Object at runtime?

if above are possible then how to do it in C#?

TIA


Nov 16 '05 #3
Yes, that would work, you are passing in a reference to IntrfaceA, and
then you are using the "is" operator to check if the underlying type of
IntfA also implements the InterfaceB interface.

Bennie Haelen
Arjang wrote:
Thanks Richard,
but what i was really after, is that only by using an interface refernce
only. for example Let MyObj implement InrefaceA.

MyObj o= new MyObj();
IntrefaceA h;
h=(InterfaceA)o;

now if I pass h to a method only accepting InterfaceA, e.g.

public MethodA( IntrfaceA IntfA )
{
//need to find out wether object passes as intfA supports InterfaceB
if (IntfA is InterfaceB) //does this work??
{
// do something using interfaceB
}

}

In Delphi I can use QueryInterface to check and convert from one interface
to another.

"Richard A. Lowe" <ch*****@yumspamyumYahoo.com> wrote in message
news:eS**************@TK2MSFTNGP12.phx.gbl...
If you want to know if any interface is supported you can just use the C#
'is' operator, i.e.

Object o = // create somthing here

if(o is IMyInterface)
{
// whatever
}

Getting all interfaces is a matter of using reflection:

Type[] interfacesIImplement = o.GetType().GetInterfaces();

--
C#, .NET and Complex Adaptive Systems:
http://blogs.geekdojo.net/Richard
"Arjang" <Ar****************@NotTheRealPart.zorg> wrote in message
news:uc**************@TK2MSFTNGP14.phx.gbl...
suppose I have an interface reference IA, to an object implementing let's
say, InterfaceA.

Is it possible to query IA and find out whether some other interface e.g.
IntefaceB is also supported?

Is it possible to get the list of all the interfaces supported from an
InterfaceRefernce and/or from an Object at runtime?

if above are possible then how to do it in C#?

TIA



Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: baylor | last post by:
In C#, an interface cannot mark any method as static. i'm told the ILASM supports it but i've never tested that Two questions. First, why? OK, i've heard the reason about interfaces being...
30
by: Frank Rizzo | last post by:
We are having one of those religious debates at work: Interfaces vs Classes. My take is that Classes give you more flexibility. You can enforce a contract on the descendant classes by marking...
8
by: John | last post by:
What is the purpose / benefit of using an interface statement? It doesn't seem like anything more than a different way to make a class... (except you can't define any procedures in an interface...
9
by: Sean Kirkpatrick | last post by:
To my eye, there doesn't seem to be a whole lot of difference between the two of them from a functional point of view. Can someone give me a good explanation of why one vs the other? Sean
18
by: _dee | last post by:
Question about best use of interfaces: Say there's a 'Master' class that needs to implement a few interfaces: class Master : I1, I2, I3 { } The actual code already exists in smaller...
22
by: RSH | last post by:
Hi, I have been reading on interfaces working on samples I've run across on the web. For the life of me I cannot seem to grasp them. It appears to me that interfaces are simply blueprints to...
18
by: Tony | last post by:
class Interface { public: virtual void DoItNow()=0; }; class A: public Interface { public: void DoItNow(); // satisfies interface explicitly
5
by: =?Utf-8?B?UmljaA==?= | last post by:
Greetings, I am actually a VB.Net guy, but I have worked somewhat with C++ and C#. I just want to ask about the relationship between Abstract Classes and Interfaces. My first question is if...
23
by: A.Gallus | last post by:
If I declare a function pure virtual: class A { virtual void myfunc() = 0; } and I derive a class from A: class B : public A
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.