Re: knowing the interfaces that an object implements



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


.



Relevant Pages

  • Re: knowing the interfaces that an object implements
    ... It does not compile this way as u can't compare a type with an object. ... > 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 ... > SqlConnection connection = new SqlConnection: ...
    (microsoft.public.dotnet.framework)
  • Re: [OT] My First C# (warning - long post)
    ... or string,. ... public string IBreturn ... interface block with the new return code. ... It is unwieldy to keep referencing substrings of an 8K string ...
    (comp.lang.cobol)
  • Re: Explicit Linking of DLLs in VB.net
    ... yes this technique will only work on managed assemblys (exe or dll) ... "YourObject" is a form and is limited to the form methods. ... Wel implement propertys, methods, events in your interface and start ... Public Shared Function LoadMeByName(ByVal vstrAssemblyName As String, ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Operator overloading [was Re: 7.0 wishlist?]
    ... I doubt Harry was proposing that the translation be naïve. ... public interface Addable{ ... BigDecimal--would be powerful enough, even if operator overloading ... String and return a String, ...
    (comp.lang.java.programmer)
  • Re: C# app with COM Interface for array of COM structs
    ... USDALib.PhysicalLocation_tI1000Gates.GetROI(int partID, string ... public interface I1000Gates ... riid, out IntPtr ppvObject) ... public interface IClassFactory ...
    (microsoft.public.dotnet.languages.csharp)

Loading