Professional Web Applications Themes

Reflection on COM object. - ASP.NET General

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 ReturnAbstractObject(ByVal o As Object) As XmlDoent Dim type As System.Type type = ...

  1. #1

    Default 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 ReturnAbstractObject(ByVal o As Object) As XmlDoent
    Dim type As System.Type
    type = o.GetType()

    Dim propertyinfo As System.Reflection.PropertyInfo

    For Each propertyinfo In type.GetProperties()

    If propertyinfo.PropertyType.FullName = "System.String" Then
    Dim s As String
    s = propertyinfo.GetValue(o, Nothing).ToString() ' HERE IT GOES
    WRONG
    End If

    Next
    :
    End Function


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

    It's the propertyinfo.GetValue(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
    [email]lnNOpentiaSPAM.dk[/email]
    (If mailing the answer, - please remove the CAPS from the email)


    Lars Nielsen Guest

  2. #2

    Default 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]
    - [email]nicholas.paldinoexisconsulting.com[/email]

    "Lars Nielsen" <lnNOpentiaSPAM.dk> wrote in message
    news:uCXFOy8SDHA.2256TK2MSFTNGP11.phx.gbl...
    > 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 ReturnAbstractObject(ByVal o As Object) As XmlDoent
    > Dim type As System.Type
    > type = o.GetType()
    >
    > Dim propertyinfo As System.Reflection.PropertyInfo
    >
    > For Each propertyinfo In type.GetProperties()
    >
    > If propertyinfo.PropertyType.FullName = "System.String" Then
    > Dim s As String
    > s = propertyinfo.GetValue(o, Nothing).ToString() ' HERE IT GOES
    > WRONG
    > End If
    >
    > Next
    > :
    > End Function
    >
    >
    > When returning an array of strings (mind you, the
    > propertyinfo.GetType().IsArray returns false) I get the following error:
    > "Parameter count mismatch".
    >
    > It's the propertyinfo.GetValue(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
    > [email]lnNOpentiaSPAM.dk[/email]
    > (If mailing the answer, - please remove the CAPS from the email)
    >
    >

    Nicholas Paldino [.NET/C# MVP] Guest

  3. #3

    Default Re: Reflection on COM object.

    Lars,
    >Until now it work's quite fine until it returns an array of strings:
    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.GetValue(o, New Object() {index}).ToString()

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



    Mattias

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

  4. #4

    Default Re: Reflection on COM object.

    Lars,
    >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?
    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
    [url]http://www.msjogren.net/dotnet/[/url]
    Please reply only to the newsgroup.
    Mattias Sjögren Guest

  5. #5

    Default 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.want.spammvps.org> wrote in message
    news:O5wGe1LTDHA.1688TK2MSFTNGP11.phx.gbl...
    > Lars,
    >
    > >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?
    >
    > 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
    > [url]http://www.msjogren.net/dotnet/[/url]
    > Please reply only to the newsgroup.

    Lars Nielsen Guest

  6. #6

    Default Re: Reflection on COM object.

    Lars,
    >Do you know any way to express an (any) Object as XML ?
    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.

    >I'm curious; Is it possible to get the length (or upperbound) from the
    >index?
    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
    [url]http://www.msjogren.net/dotnet/[/url]
    Please reply only to the newsgroup.
    Mattias Sjögren Guest

  7. #7

    Default 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.want.spammvps.org> wrote in message
    news:u82FdwQTDHA.3132tk2msftngp13.phx.gbl...
    > Lars,
    >
    > >Do you know any way to express an (any) Object as XML ?
    >
    > 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.
    >
    >
    > >I'm curious; Is it possible to get the length (or upperbound) from the
    > >index?
    >
    > 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
    > [url]http://www.msjogren.net/dotnet/[/url]
    > Please reply only to the newsgroup.

    Lars Nielsen Guest

Similar Threads

  1. Reflection map?
    By Pelleyoo in forum Macromedia Director 3D
    Replies: 6
    Last Post: April 16th, 05:09 PM
  2. Problems with reflection
    By Holger Mescheder in forum Macromedia Director 3D
    Replies: 2
    Last Post: November 19th, 11:49 AM
  3. Self Reflection
    By Beth Haney in forum Adobe Photoshop Elements
    Replies: 3
    Last Post: August 1st, 02:41 PM
  4. Reflection and SqlConnection
    By WebDevHokie in forum ASP.NET General
    Replies: 1
    Last Post: July 10th, 06:38 PM
  5. [PHP-DEV] The PHP5 Reflection API
    By s0niX in forum PHP Development
    Replies: 0
    Last Post: July 9th, 10:39 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139