Finding which interfaces an instance implements

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

    Finding which interfaces an instance implements

    Given:

    interface ICompass{}
    interface INorth : ICompass{ ... stuff ...}
    interface ISouth : ICompass{ ... stuff ... }
    interface IEast : ICompass { .. stuff ...}
    interface IWest : ICompass { .. stuff ... }

    interface Box{ ... stuff ... };
    interface Carton { ... stuff ...};
    interface Bag { ... stuff ...}

    and a whole series of classes that implement them, such as

    class Thing : INorth, ICarton { ... stuff ... }
    class AnotherThing : ISouth, IBag { ... stuff ... }

    how can I find out whether two instances have the same Compass interface
    without resorting to

    if (left is INorth)
    return right is INorth
    else if (left is ISouth)
    return right is ISouth
    else ...

    TIA,

    Andrew


  • Paul E Collins

    #2
    Re: Finding which interfaces an instance implements

    amaca wrote:
    how can I find out whether two instances have the
    same Compass interface without resorting to
    if (left is INorth)
    return right is INorth
    [...]
    You could do some fancy tricks with reflection to navigate the
    class/interface hierarchy at runtime - e.g. comparing the items in
    left.GetType(). GetInterfaces() and right.GetType() .GetInterfaces( ).

    Failing that, there are simpler hacks that aren't entirely hateful.
    For example, you could include a method GetUniqueValue in the Compass
    interface, ensure that each implementor really does have a unique
    value for it (perhaps its own class name), and then just test
    left.GetUniqueV alue() == right.GetUnique Value().

    Eq.


    Comment

    • Nicholas Paldino [.NET/C# MVP]

      #3
      Re: Finding which interfaces an instance implements

      You can do this easily with the IsAssignableFro m method on the Type
      class. Basically, you can create the following methods to do this for you:

      static bool InstancesImplem entCompassInter face(object instanceA, object
      instanceB, Type compassInterfac eType) {
      // Check instanceA and instanceB, make sure they are not null.
      ...

      // Call the overload.
      return InstancesImplem entCompassInter face(instanceA. GetType(),
      instanceB.GetTy pe(),
      compassInterfac eType);
      }

      static bool InstancesImplem entCompassInter face(Type typeA, Type typeB, Type
      compassInterfac eType)
      {
      // Check compassInterfac eType here, make sure that it derives from
      ICompass.
      if (!typeof(ICompa ss).IsAssignabl eFrom(compassIn terfaceType))
      {
      // Throw an exception here.
      }

      // Check the types for null here.

      // See if the instances are assignable to the interface type.
      return (compassInterfa ceType.IsAssign ableFrom(typeA) &&
      compassInterfac eType.IsAssigna bleFrom(typeB)) ;
      }

      Hope this helps.


      --
      - Nicholas Paldino [.NET/C# MVP]
      - mvp@spam.guard. caspershouse.co m

      "Paul E Collins" <find_my_real_a ddress@CL4.orgw rote in message
      news:8PGdne4Y_a jlFU7ZnZ2dnUVZ8 qOdnZ2d@bt.com. ..
      amaca wrote:
      >
      >how can I find out whether two instances have the
      >same Compass interface without resorting to
      >if (left is INorth)
      > return right is INorth
      >[...]
      >
      You could do some fancy tricks with reflection to navigate the
      class/interface hierarchy at runtime - e.g. comparing the items in
      left.GetType(). GetInterfaces() and right.GetType() .GetInterfaces( ).
      >
      Failing that, there are simpler hacks that aren't entirely hateful. For
      example, you could include a method GetUniqueValue in the Compass
      interface, ensure that each implementor really does have a unique value
      for it (perhaps its own class name), and then just test
      left.GetUniqueV alue() == right.GetUnique Value().
      >
      Eq.
      >
      >

      Comment

      • amaca

        #4
        Re: Finding which interfaces an instance implements

        Thank you both, both for the way of doing it using reflection and for the
        not-so-hateful hacks. I could use either, but implementing a CompassType for
        each class turns out to be helpful for working out what's going on in the
        debugger, and so that's the way I'll go.

        Andrew


        Comment

        Working...