Relection on interfaces

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jupitermoonbeam@googlemail.com

    Relection on interfaces

    Hello,

    I wondered if anyone could help. I wish to retrieve the PropertyInfo[]
    of a specific interface but can't work out how to do this in a type
    safe way.

    Basically I don't want to do this:
    void Reflect(IMyObje ct objectToReflect )
    {
    PropertyInfo[] myObjectPropert ies =
    objectToReflect .GetType().GetP roperties();
    }

    Reason being it may have multiple interfaces or it's own properties and
    the reflection is based on the object not on the parameter type (i.e.
    GetType() returns will return object type which inherits the interface
    not the interface itself).

    So what I'm trying to do is this instead:
    void Reflect(IMyObje ct objectToReflect )
    {
    PropertyInfo[] myObjectPropert ies =
    Type.GetType(IM yObject).GetPro perties();
    }

    But I can't find the code to do it (unless I use a string - but then
    that's not type safe).

    Anyone have any ideas?

    Most grateful.

  • Joanna Carter [TeamB]

    #2
    Re: Relection on interfaces

    <jupitermoonbea m@googlemail.co ma écrit dans le message de news:
    1158924965.3166 76.274410@d34g2 00...legr oups.com...

    | So what I'm trying to do is this instead:
    | void Reflect(IMyObje ct objectToReflect )
    | {
    | PropertyInfo[] myObjectPropert ies =
    | Type.GetType(IM yObject).GetPro perties();
    | }

    Try this :

    void Reflect(IMyObje ct objectToReflect )
    {
    PropertyInfo[] myObjectPropert ies = typeof(IMyObjec t).GetPropertie s();
    }

    Joanna

    --
    Joanna Carter [TeamB]
    Consultant Software Engineer


    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: Relection on interfaces

      jupitermoonbeam @googlemail.com <jupitermoonbea m@googlemail.co mwrote:
      I wondered if anyone could help. I wish to retrieve the PropertyInfo[]
      of a specific interface but can't work out how to do this in a type
      safe way.
      >
      Basically I don't want to do this:
      void Reflect(IMyObje ct objectToReflect )
      {
      PropertyInfo[] myObjectPropert ies =
      objectToReflect .GetType().GetP roperties();
      }
      >
      Reason being it may have multiple interfaces or it's own properties and
      the reflection is based on the object not on the parameter type (i.e.
      GetType() returns will return object type which inherits the interface
      not the interface itself).
      >
      So what I'm trying to do is this instead:
      void Reflect(IMyObje ct objectToReflect )
      {
      PropertyInfo[] myObjectPropert ies =
      Type.GetType(IM yObject).GetPro perties();
      }
      >
      But I can't find the code to do it (unless I use a string - but then
      that's not type safe).
      I think what you're after is:

      void Reflect (Type interfaceType, object targetObject)
      {
      PropertyInfo[] properties = interfaceType.G etProperties();
      // use properties with targetObject
      }

      Then you'd use it with:

      Reflect (typeof(IMyObje ct), myObject)

      --
      Jon Skeet - <skeet@pobox.co m>
      http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
      If replying to the group, please do not mail me too

      Comment

      • jupitermoonbeam@googlemail.com

        #4
        Re: Relection on interfaces

        Thanks very much.

        I know it sounds stupid but I had one of those typical developer "I
        swear I tried that and it didn't work". Not sure how I missed it but
        thanks for the help!

        Joanna Carter [TeamB] wrote:
        <jupitermoonbea m@googlemail.co ma écrit dans le message de news:
        1158924965.3166 76.274410@d34g2 00...legr oups.com...
        >
        | So what I'm trying to do is this instead:
        | void Reflect(IMyObje ct objectToReflect )
        | {
        | PropertyInfo[] myObjectPropert ies =
        | Type.GetType(IM yObject).GetPro perties();
        | }
        >
        Try this :
        >
        void Reflect(IMyObje ct objectToReflect )
        {
        PropertyInfo[] myObjectPropert ies = typeof(IMyObjec t).GetPropertie s();
        }
        >
        Joanna

        --
        Joanna Carter [TeamB]
        Consultant Software Engineer

        Comment

        • Joanna Carter [TeamB]

          #5
          Re: Relection on interfaces

          <jupitermoonbea m@googlemail.co ma écrit dans le message de news:
          1159267753.2991 47.190650@b28g2 00...legr oups.com...

          I know it sounds stupid but I had one of those typical developer "I
          swear I tried that and it didn't work". Not sure how I missed it but
          thanks for the help!

          No problem, I've just about printed the tee-shirt on that one :-)

          Joanna

          --
          Joanna Carter [TeamB]
          Consultant Software Engineer


          Comment

          Working...