GetInterface() without a string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gomaw Beoyr

    #1

    GetInterface() without a string


    If I have an object o and want to check if it's a certain interface,
    I write like this:

    bool oIsIMyInterface = o is IMyInterface;

    However, if I have a Type object t, and I want to check if it's a
    certain interface, I have to write the interface name as a string:

    bool tIsIMyInterface = t.GetInterface( "MyNamespace.IM yInterface") != null;

    Is there any way to do this without putting the interface between
    quotes, i.e. without writing it as a string, i.e. getting it
    type-checked etc by the compiler?

    Something along these lines:

    bool tIsIMyInterface = t.IsSubclassOf( typeof(IMyInter face)) != null;


    Regards,
    /Gomaw

  • Rob Tillie

    #2
    Re: GetInterface() without a string

    Hey,

    you can do this with the object itself. When you have an object o which
    implements an interface i,
    "o is i" will return true. So you can write: if(o is i) { // o implements
    the interface i}
    For further info lookup the is ( and as) operators.

    Greetz,
    -- Rob.

    Gomaw Beoyr wrote:[color=blue]
    > If I have an object o and want to check if it's a certain interface,
    > I write like this:
    >
    > bool oIsIMyInterface = o is IMyInterface;
    >
    > However, if I have a Type object t, and I want to check if it's a
    > certain interface, I have to write the interface name as a string:
    >
    > bool tIsIMyInterface = t.GetInterface( "MyNamespace.IM yInterface")
    > != null;
    >
    > Is there any way to do this without putting the interface between
    > quotes, i.e. without writing it as a string, i.e. getting it
    > type-checked etc by the compiler?
    >
    > Something along these lines:
    >
    > bool tIsIMyInterface = t.IsSubclassOf( typeof(IMyInter face)) !=
    > null;
    >
    >
    > Regards,
    > /Gomaw[/color]


    Comment

    • Mike Schilling

      #3
      Re: GetInterface() without a string

      "Gomaw Beoyr" <Gomaw.Beoyr@no .spam.please.no > wrote in message
      news:OHVIHyJXDH A.384@TK2MSFTNG P12.phx.gbl...[color=blue]
      >
      > If I have an object o and want to check if it's a certain interface,
      > I write like this:
      >
      > bool oIsIMyInterface = o is IMyInterface;
      >
      > However, if I have a Type object t, and I want to check if it's a
      > certain interface, I have to write the interface name as a string:
      >
      > bool tIsIMyInterface = t.GetInterface( "MyNamespace.IM yInterface") !=[/color]
      null;[color=blue]
      >
      > Is there any way to do this without putting the interface between
      > quotes, i.e. without writing it as a string, i.e. getting it
      > type-checked etc by the compiler?
      >
      > Something along these lines:
      >
      > bool tIsIMyInterface = t.IsSubclassOf( typeof(IMyInter face)) != null;[/color]

      Check out System.Type.IsA ssignableFrom() .


      Comment

      • Gomaw Beoyr

        #4
        Re: GetInterface() without a string

        In article <eFbAMcKXDHA.13 84@TK2MSFTNGP10 .phx.gbl>,
        Rob.Tillie@stud ent.tul.edu (Rob Tillie) writes:
        [color=blue]
        > you can do this with the object itself. When you have an object o which
        > implements an interface i,
        > "o is i" will return true. So you can write: if(o is i) { // o implements
        > the interface i}[/color]

        Yes, thanks, but my problem was that I don't have an object. The
        program checks if the class (represented by a Type object) implements
        the interface, and if it does, an object is instantiated with
        Activator.Creat eInstance().

        The way to solve the problem was posted by Mike Schilling, where t
        is the Type object:

        bool tIsIMyInterface = typeof(IMyInter face).IsAssigna bleFrom(t);

        (I assume that the only way an interface can be assignable from a
        type is if the type implements the interface...)

        Gomaw

        Comment

        Working...