Converion between interfaces

  • Thread starter Thread starter Arjang
  • Start date Start date
A

Arjang

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
 
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();
 
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 said:
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 said:
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
 
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

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.



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
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
 
Back
Top