Reflection on COM object.

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

    #1

    Reflection on COM object.

    Hi everybody,

    I've run into a problem when using the reflection assembly on COM objects
    and been browsing around the net for some while. Apparently a few has asked
    the same question though no answer has ever occoured.

    This is what I'm trying to do:

    I wish to build a function that crawls any COM object (interop). I did
    shortly examine the serializer until I realized it's used for translation
    and not reflection). Until now it work's quite fine until it returns an
    array of strings:

    Public Function ReturnAbstractO bject(ByVal o As Object) As XmlDocument
    Dim type As System.Type
    type = o.GetType()

    Dim propertyinfo As System.Reflecti on.PropertyInfo

    For Each propertyinfo In type.GetPropert ies()

    If propertyinfo.Pr opertyType.Full Name = "System.Str ing" Then
    Dim s As String
    s = propertyinfo.Ge tValue(o, Nothing).ToStri ng() ' HERE IT GOES
    WRONG
    End If

    Next
    :
    End Function


    When returning an array of strings (mind you, the
    propertyinfo.Ge tType().IsArray returns false) I get the following error:
    "Parameter count mismatch".

    It's the propertyinfo.Ge tValue(o, Nothing) that raises an error. I cannot
    find an example (that works) where the second parameter are used (this is
    the Index parameter). I may be wrong in the entire approch, and would gladly
    receive any help you guys may offer.

    In advance thanks,

    Lars Nielsen
    ln@NOpentiaSPAM .dk
    (If mailing the answer, - please remove the CAPS from the email)


  • Mattias Sjögren

    #2
    Re: Reflection on COM object.

    Lars,
    [color=blue]
    >Until now it work's quite fine until it returns an array of strings:[/color]

    It sounds more like you're seeing a parameterized property (a.k.a.
    indexer in C#), that takes an index argument and returns a single
    string, as opposed to returning an entire array. That would explain
    why IsArray for the property type returns false, and why you get the
    exception.

    If the property takes an integer argument specifying the index, you
    should be able to query it like this

    s = propertyinfo.Ge tValue(o, New Object() {index}).ToStri ng()

    You can find out more about the parmeters a property takes by checking
    PropertyInfo.Ge tIndexParameter s().



    Mattias

    --
    Mattias Sjögren [MVP] mattias @ mvps.org

    Please reply only to the newsgroup.

    Comment

    • Nicholas Paldino [.NET/C# MVP]

      #3
      Re: Reflection on COM object.

      Lars,

      The second parameter should be used for indexers, which is not what you
      want to use, a property that exposes an array of strings is a regular
      property, not an indexer. You should not use this overload. What is the
      COM signature of the property?


      --
      - Nicholas Paldino [.NET/C# MVP]
      - nicholas.paldin o@exisconsultin g.com

      "Lars Nielsen" <ln@NOpentiaSPA M.dk> wrote in message
      news:uCXFOy8SDH A.2256@TK2MSFTN GP11.phx.gbl...[color=blue]
      > Hi everybody,
      >
      > I've run into a problem when using the reflection assembly on COM objects
      > and been browsing around the net for some while. Apparently a few has[/color]
      asked[color=blue]
      > the same question though no answer has ever occoured.
      >
      > This is what I'm trying to do:
      >
      > I wish to build a function that crawls any COM object (interop). I did
      > shortly examine the serializer until I realized it's used for translation
      > and not reflection). Until now it work's quite fine until it returns an
      > array of strings:
      >
      > Public Function ReturnAbstractO bject(ByVal o As Object) As XmlDocument
      > Dim type As System.Type
      > type = o.GetType()
      >
      > Dim propertyinfo As System.Reflecti on.PropertyInfo
      >
      > For Each propertyinfo In type.GetPropert ies()
      >
      > If propertyinfo.Pr opertyType.Full Name = "System.Str ing" Then
      > Dim s As String
      > s = propertyinfo.Ge tValue(o, Nothing).ToStri ng() ' HERE IT GOES
      > WRONG
      > End If
      >
      > Next
      > :
      > End Function
      >
      >
      > When returning an array of strings (mind you, the
      > propertyinfo.Ge tType().IsArray returns false) I get the following error:
      > "Parameter count mismatch".
      >
      > It's the propertyinfo.Ge tValue(o, Nothing) that raises an error. I cannot
      > find an example (that works) where the second parameter are used (this is
      > the Index parameter). I may be wrong in the entire approch, and would[/color]
      gladly[color=blue]
      > receive any help you guys may offer.
      >
      > In advance thanks,
      >
      > Lars Nielsen
      > ln@NOpentiaSPAM .dk
      > (If mailing the answer, - please remove the CAPS from the email)
      >
      >[/color]


      Comment

      • Mattias Sjögren

        #4
        Re: Reflection on COM object.

        Lars,
        [color=blue]
        >However, this does not seem to supply any method to get the length of the
        >index. Do you, or anybody else, know any way to extract such a value?[/color]

        Not all parameterized properties wrap arrays, so index parameters
        might not be numeric, and there can be more than one. So it's close to
        impossible to write generic code that queries such properties without
        any knowledge of the implementation.



        Mattias

        --
        Mattias Sjögren [MVP] mattias @ mvps.org

        Please reply only to the newsgroup.

        Comment

        • Lars Nielsen

          #5
          Re: Reflection on COM object.

          Yes, that's what I feared.

          Do you know any way to express an (any) Object as XML ?

          I'm curious; Is it possible to get the length (or upperbound) from the
          index?

          By the way, thanks for the help. It has clearifyed many "dimmed" areas.


          -- Lars


          "Mattias Sjögren" <mattias.dont.w ant.spam@mvps.o rg> wrote in message
          news:O5wGe1LTDH A.1688@TK2MSFTN GP11.phx.gbl...[color=blue]
          > Lars,
          >[color=green]
          > >However, this does not seem to supply any method to get the length of the
          > >index. Do you, or anybody else, know any way to extract such a value?[/color]
          >
          > Not all parameterized properties wrap arrays, so index parameters
          > might not be numeric, and there can be more than one. So it's close to
          > impossible to write generic code that queries such properties without
          > any knowledge of the implementation.
          >
          >
          >
          > Mattias
          >
          > --
          > Mattias Sjögren [MVP] mattias @ mvps.org
          > http://www.msjogren.net/dotnet/
          > Please reply only to the newsgroup.[/color]


          Comment

          • Mattias Sjögren

            #6
            Re: Reflection on COM object.

            Lars,
            [color=blue]
            >Do you know any way to express an (any) Object as XML ?[/color]

            No, I don't. Usually COM objects take care of their own persistance
            and exposes it by implementing one or more of the IPersist interfaces.

            [color=blue]
            >I'm curious; Is it possible to get the length (or upperbound) from the
            >index?[/color]

            I guess you could look for a property called Count or Length or
            something like that. But that wouldn't work in all situations.



            Mattias

            --
            Mattias Sjögren [MVP] mattias @ mvps.org

            Please reply only to the newsgroup.

            Comment

            • Lars Nielsen

              #7
              Re: Reflection on COM object.

              Mattias,

              That will off course pose as a problem when making a general COM object
              reflection class (that dumps all values). You must then know something about
              the object.

              Thank you very much for your help. When my class is done, I will do a repost
              as a Q - > A as a help to others that tries the same.


              Best regards,

              Lars Fløe Nielsen




              "Mattias Sjögren" <mattias.dont.w ant.spam@mvps.o rg> wrote in message
              news:u82FdwQTDH A.3132@tk2msftn gp13.phx.gbl...[color=blue]
              > Lars,
              >[color=green]
              > >Do you know any way to express an (any) Object as XML ?[/color]
              >
              > No, I don't. Usually COM objects take care of their own persistance
              > and exposes it by implementing one or more of the IPersist interfaces.
              >
              >[color=green]
              > >I'm curious; Is it possible to get the length (or upperbound) from the
              > >index?[/color]
              >
              > I guess you could look for a property called Count or Length or
              > something like that. But that wouldn't work in all situations.
              >
              >
              >
              > Mattias
              >
              > --
              > Mattias Sjögren [MVP] mattias @ mvps.org
              > http://www.msjogren.net/dotnet/
              > Please reply only to the newsgroup.[/color]


              Comment

              Working...