Check if Object implements an Interface?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lucas Tam

    #1

    Check if Object implements an Interface?

    Hi all,

    I like to dynamically check if an object implements a particular interface.

    Currently I am doing:

    Ctype(Object, IInterface)

    If the object implements the interface it will not throw an error... Is
    there a better way I can do this via System.Reflecti on?

    Thanks!

    --
    Lucas Tam (REMOVEnntp@rog ers.com)
    Please delete "REMOVE" from the e-mail address when replying.
    http://members.ebay.com/aboutme/coolspot18/
  • Lucas Tam

    #2
    Re: Check if Object implements an Interface?

    Lucas Tam <REMOVEnntp@rog ers.com> wrote in
    news:Xns96A1D10 84D3CDnntproger scom@127.0.0.1:
    [color=blue]
    > Hi all,
    >
    > I like to dynamically check if an object implements a particular
    > interface.[/color]

    Found the answer on groups.google.c om : )

    GetType.GetInte rface()

    --
    Lucas Tam (REMOVEnntp@rog ers.com)
    Please delete "REMOVE" from the e-mail address when replying.
    http://members.ebay.com/aboutme/coolspot18/

    Comment

    • Mattias Sjögren

      #3
      Re: Check if Object implements an Interface?

      >If the object implements the interface it will not throw an error... Is[color=blue]
      >there a better way I can do this via System.Reflecti on?[/color]

      Why do you want to use Reflection? The easiest way is

      If TypeOf obj is IInterace Then



      Mattias

      --
      Mattias Sjögren [MVP] mattias @ mvps.org
      http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
      Please reply only to the newsgroup.

      Comment

      • Herfried K. Wagner [MVP]

        #4
        Re: Check if Object implements an Interface?

        "Lucas Tam" <REMOVEnntp@rog ers.com> schrieb:[color=blue][color=green]
        >> I like to dynamically check if an object implements a particular
        >> interface.[/color]
        >
        > Found the answer on groups.google.c om : )
        >
        > GetType.GetInte rface()[/color]

        I suggest to use 'TypeOf...Is' when possible.

        --
        M S Herfried K. Wagner
        M V P <URL:http://dotnet.mvps.org/>
        V B <URL:http://classicvb.org/petition/>

        Comment

        • Lucas Tam

          #5
          Re: Check if Object implements an Interface?

          Mattias Sjögren <mattias.dont.w ant.spam@mvps.o rg> wrote in
          news:ullMTkBlFH A.1048@tk2msftn gp13.phx.gbl:
          [color=blue]
          > Why do you want to use Reflection? The easiest way is
          >
          > If TypeOf obj is IInterace Then[/color]

          Oh Ha Ha... I didn't think TypeOf would check it's interfaces... I always
          thought TypeOf only checked it's object type : )

          --
          Lucas Tam (REMOVEnntp@rog ers.com)
          Please delete "REMOVE" from the e-mail address when replying.
          http://members.ebay.com/aboutme/coolspot18/

          Comment

          Working...