473,386 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 XmlDocument
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
ln@NOpentiaSPAM.dk
(If mailing the answer, - please remove the CAPS from the email)
Nov 13 '05 #1
6 5923
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
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 13 '05 #2
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]
- ni**************@exisconsulting.com

"Lars Nielsen" <ln@NOpentiaSPAM.dk> wrote in message
news:uC**************@TK2MSFTNGP11.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 XmlDocument
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
ln@NOpentiaSPAM.dk
(If mailing the answer, - please remove the CAPS from the email)

Nov 13 '05 #3
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
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 13 '05 #4
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" <ma********************@mvps.org> wrote in message
news:O5**************@TK2MSFTNGP11.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
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.

Nov 13 '05 #5
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
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 13 '05 #6
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" <ma********************@mvps.org> wrote in message
news:u8**************@tk2msftngp13.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
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.

Nov 13 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Derek Hart | last post by:
I wish to execute code from a string. The string will have a function name, which will return a string: Dim a as string a = "MyFunctionName(param1, param2)" I have seen a ton of people...
4
by: Tim Werth | last post by:
I am trying to use reflection to add an event handler for the RowUpdated event of the OracleDataAdapter object (ODP.NET), but the same thing can be said for SqlDataAdapter if you only use...
10
by: Sunny | last post by:
Hi, I have an old problem which I couldn't solve so far. Now I have found a post in that group that gave me an idea, but I can not fully understand it. The problem is: I'm trying to use a...
2
by: Jason Coyne Gaijin42 | last post by:
I have seen several people looking for a way to access the Columns collection when using the AutoGenerate = true option. Some people have gotten so far as to find the private autoGenColumnsArray...
11
by: Aaron Queenan | last post by:
Given the classes: class Class { public static implicit operator int(Class c) { return 0; } } class Holder
2
by: Jeff | last post by:
I am trying to dynamically load an assembly via reflection and then invoke a method of that assembly that will populate a custom type collection passed into the method byref. I am able to...
2
by: diego | last post by:
hi everyone, i have a sub that opens a form given the form's name as string and opens it using System.Reflection. How can I set the form's properties at runtime. Here is my code. Public Sub...
5
by: Klaudiusz Bryja | last post by:
Hi, This is for NetCF 2.0. I need to create event handling code which using reflection. I have some parameters in XML which describe how event should be handled. I have code to create...
6
by: =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post by:
Hi I am slightly familiar with reflection but have never done the following I know how to find a class and call but I haven't done the following The Method return a List of Another Class And...
6
by: Cralis | last post by:
Hi guys, Someone once said, 'You can do that with reflection'. I can't recall what it was I was trying to do at the time, but then he said, 'Any developer knows what reflection is...'. I kept...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.