knowing the interfaces that an object implements

  • Thread starter Thread starter Sanjay
  • Start date Start date
S

Sanjay

hi all,

i have a function that accepts Object as an argument.Depending on whether
the passed object
implements Idisposable , i want to call dispose on that object.
How can i check whether this object implements Idisposable ?

I would be thankful for any pointer on the same.

sanjay
 
To check wether the interface IDisposable is implemented:

if(<object>.GetType().GetInterface("IDisposable") == null)
return false;
else
return true;


To get all interfaces of an object:


Type[] interfaces = <object>.GetType().GetInterfaces();
foreach(Type _interface in interfaces)
{
Console.WriteLine(_interface.Name);
}


Gabriel Lozano-Morán
 
In this case the following is probably faster (no need to use reflection):

IDisposable disp = o as IDisposable;
if (disp != null)
{
disp.Dispose();
}

Regards, Jakob.
 
if(o is IDisposable){

((IDisposable)o).Dispose();
}

Tiberiu Covaci.

Jakob Christensen said:
In this case the following is probably faster (no need to use reflection):

IDisposable disp = o as IDisposable;
if (disp != null)
{
disp.Dispose();
}

Regards, Jakob.


LOZANO-MORÁN said:
To check wether the interface IDisposable is implemented:

if(<object>.GetType().GetInterface("IDisposable") == null)
return false;
else
return true;


To get all interfaces of an object:


Type[] interfaces = <object>.GetType().GetInterfaces();
foreach(Type _interface in interfaces)
{
Console.WriteLine(_interface.Name);
}


Gabriel Lozano-Morán
 
As you can see there are several ways to do this :) it all depends on what
you want.

If you only need to check for the IDisposable interface I would indeed
choose for Tiberius solution but you don't need to explicitly cast to
IDisposable first because if the validation is true you are already sure
that the Dispose() method exists.

Eg:
SqlConnection connection = new SqlConnection():

if(connection is IDisposable)
connection.Dispose();

My solution was from a method I had written that accepts 2 parameters, the
object to check and the name of an interface:

Eg:
public static bool ExistsInterface(object objectToCheck, string
interfaceName)
{
if(objectToCheck.GetType().GetInterface(interfaceName) == null)
return false;
else
return true;
}


Gabriel Lozano-Morán
 
hi,
can u give me the vb .net equivalent of this code ?
if(connection is IDisposable)
connection.Dispose();
It does not compile this way as u can't compare a type with an object.

thank u.

sanjay
 
Or how about writing two methods, and letting the runtime decide:

│ public void
│ DisposeIt
│ (
│ IDisposable Subject
│ )
│ {
│ Subject.Dispose() ;
│ }
│
│ public void
│ DisposeIt
│ (
│ object Subject
│ )
│ {
│ }
 
In terms of efficiency you should prefer the

IDisposable d = connection as IDisposable;
if( d != null )
{
d.Dispose();
}

as the is/cast construct performs the runtime type check twice (once for the is once for the cast). Checking for null is alot cheaper than performing a runtime type check.

IIRC the VB code is something like

Dim d as IDisposable = CType(connection, IDisposable)
if not d is nothing then
d.Dispose()
end if

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

hi,
can u give me the vb .net equivalent of this code ?
if(connection is IDisposable)
connection.Dispose();
It does not compile this way as u can't compare a type with an object.

thank u.

sanjay
 
Jakob Christensen said:
I am no VB.net expert but I believe the following will work:

If TypeOf connection Is IDisposable Then
connection.Dispose()
End If

I don't believe it should, if Option Strict is on. The compiler still
doesn't know that connection implements IDisposable - it doesn't
understand the consequences of the "if" passing.
 
PIEBALD said:
Or how about writing two methods, and letting the runtime decide:

That would only work if overloading were performed at runtime, rather
than compile-time. You could easily have a call which didn't know at
compile-time that it was passing something implementing IDisposable, so
the call with the "object" parameter would be chosen, and Dispose
wouldn't get called.
 
It's not evaluated until runtime - therefore it actually does work. It uses
the IL instruction isinst to make the determination.

Hope this helps,

Nick Hall
 
Nick Hall said:
It's not evaluated until runtime - therefore it actually does work. It uses
the IL instruction isinst to make the determination.

The "if" does, but the call to Dispose should fail at compile time,
because the compiler doesn't know that connection implements
IDisposable. Of course, with Option Strict turned off it would be okay,
but personally I think type-safety is rather important :)
 
If the connection variable is declared as Object the VB compiler
unfortunately does not complain even with Option Explicit on. This is one of
the "features" of VB.net (and one of the reasons that I prefer C# :-))

Regards, Jakob.
 
Well what do you know I completely missed the lack of explicit cast! Talk
about seeing what you expect to see :-)

Nick Hall
 
Jakob Christensen said:
If the connection variable is declared as Object the VB compiler
unfortunately does not complain even with Option Explicit on. This is one of
the "features" of VB.net (and one of the reasons that I prefer C# :-))

Strict and Explicit are somewhat different. Try to compile the code
below:

Option Strict On

Module Hello
Sub Main()
Dim x as Object
x.Dispose()
End Sub
End Module

You get the compiler error:

c:\test\Test.vb(8) : error BC30574: Option Strict On disallows late
binding.

x.Dispose()
~~~~~~~~~~~
 
Back
Top