473,659 Members | 2,922 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reflection with array

I want to deserialize a file. In one ofe my classes I have an array
like:

public class MyClass
{
public MyArray[] theArray;
}

i.e. an arry of an own defined class. While reading the file I only know
the dimension on the end of reading, so I keep the array elements in an
ArrayList "records". At the end I "only" have to assign the ArrayList to
the Array-field "theArray". And there is my problem.

arrayField.Fiel dType shows "MyArray[]", this is correct for me. If I try
with

arrayField.SetV alue(records.To Array(), instanceOfMyCla ss);

to assign the Array I get the error message that the objecttype can't be
converted to the destination type.
That's right, the object type is "object[]" and the destination type is
"MyArray[]".

How can I do the typecast at runtime?
Or is there another way to do this?

--
Sleepless in Berlin
Wernfried
Nov 16 '05 #1
4 1997
Hi Wernfried,

Before digging more deeply into this.

Are you sure that the invalid cast is because of the arrays
arrayField.SetV alue(records.To Array(), instanceOfMyCla ss);
AFIK in FiledInfo.SetVa lue first comes the object which field you set and
then the value of the field. So I believe you should switch the paramers

arrayField.SetV alue(instanceOf MyClass, records.ToArray ());

Try that and tell if the probles is still there

--
HTH
Stoitcho Goutsev (100) [C# MVP]
"Wernfried Schwenkner" <we************ *************@s esa.de> wrote in
message news:ol******** ***@news.bln.se sa.de... I want to deserialize a file. In one ofe my classes I have an array
like:

public class MyClass
{
public MyArray[] theArray;
}

i.e. an arry of an own defined class. While reading the file I only know
the dimension on the end of reading, so I keep the array elements in an
ArrayList "records". At the end I "only" have to assign the ArrayList to
the Array-field "theArray". And there is my problem.

arrayField.Fiel dType shows "MyArray[]", this is correct for me. If I try
with

arrayField.SetV alue(records.To Array(), instanceOfMyCla ss);

to assign the Array I get the error message that the objecttype can't be
converted to the destination type.
That's right, the object type is "object[]" and the destination type is
"MyArray[]".

How can I do the typecast at runtime?
Or is there another way to do this?

--
Sleepless in Berlin
Wernfried

Nov 16 '05 #2
Hi Wernfried,

Before digging more deeply into this.

Are you sure that the invalid cast is because of the arrays
arrayField.SetV alue(records.To Array(), instanceOfMyCla ss);
AFIK in FiledInfo.SetVa lue first comes the object which field you set and
then the value of the field. So I believe you should switch the paramers

arrayField.SetV alue(instanceOf MyClass, records.ToArray ());

Try that and tell if the probles is still there

--
HTH
Stoitcho Goutsev (100) [C# MVP]
"Wernfried Schwenkner" <we************ *************@s esa.de> wrote in
message news:ol******** ***@news.bln.se sa.de... I want to deserialize a file. In one ofe my classes I have an array
like:

public class MyClass
{
public MyArray[] theArray;
}

i.e. an arry of an own defined class. While reading the file I only know
the dimension on the end of reading, so I keep the array elements in an
ArrayList "records". At the end I "only" have to assign the ArrayList to
the Array-field "theArray". And there is my problem.

arrayField.Fiel dType shows "MyArray[]", this is correct for me. If I try
with

arrayField.SetV alue(records.To Array(), instanceOfMyCla ss);

to assign the Array I get the error message that the objecttype can't be
converted to the destination type.
That's right, the object type is "object[]" and the destination type is
"MyArray[]".

How can I do the typecast at runtime?
Or is there another way to do this?

--
Sleepless in Berlin
Wernfried

Nov 16 '05 #3
In article <uw************ **@TK2MSFTNGP12 .phx.gbl>, 10*@100.com says...

arrayField.SetV alue(instanceOf MyClass, records.ToArray ());

Try that and tell if the probles is still there

Thanks. This was the problem. I certainly got confused with my variabale
names and missed the order.

--
Sleepless in Berlin
Wernfried
Nov 16 '05 #4
In article <uw************ **@TK2MSFTNGP12 .phx.gbl>, 10*@100.com says...

arrayField.SetV alue(instanceOf MyClass, records.ToArray ());

Try that and tell if the probles is still there

Thanks. This was the problem. I certainly got confused with my variabale
names and missed the order.

--
Sleepless in Berlin
Wernfried
Nov 16 '05 #5

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

Similar topics

6
5937
by: Lars Nielsen | last post by:
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
10
7360
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 Windows.Forms.UserControl in a COM environment, i.e. I want to host that control in a COM host. So far, so good, I can host it, but I can not reach the parent COM object from the control (Parent property is null :( ). I have stopped the control in the...
2
864
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 that has the information, but we as developers have no way to access this information. I have come up with a solution for the problem, (as I am sure many others have) using reflection. Here is some sample code that will print out the auto...
3
4199
by: trevorelbourne | last post by:
Hi, I am having trouble accessing the elements of an array using reflection. This is the code I am having trouble with: FieldInfo Fields = Obj.GetType().GetFields(); foreach (FieldInfo fi in Fields) { Object Temp = fi.GetValue(Obj); if (Temp.GetType().IsArray)
12
6987
by: Gus | last post by:
Hi, I´m triying to invoke a member using reflection, but this member requiere two parameters, the first patrameter is a string and the second is a reference to a dataset (output parameter). The parameters has to be passed in an array of objects. I´ve tried something like this: object parameters = new object; parameters ="text";
1
2277
by: Rathish P S | last post by:
Hi friends, I am trying with Reflection and Generics in C#. But I get confused with some of these new features. I am trying to get the type of generic parameter with the type.Assembly.GetType(name,false,false) method. It is return as null. Can anybody tell can help me to find the basic reason of this issue? this is the sample code that I used. using System; using System.Collections.Generic;
2
1908
by: Lev | last post by:
Hi, I have some code that does reflection on an assembly I load. When I try to get the attributes on one of the methods implemented in the assembly, the MC++ version does not return anything. See code below: Object __gc* memberAttributes = info->GetCustomAttributes(__typeof(EntryPointAttribute), false);
1
1727
by: Stabiplan BV | last post by:
Hi, I am trying to find const variables outside a class. Thur far I did not succeed. Without const, the variable is found but with const not. So, how can I get hold on global const variables using reflection? Regards, Rob
0
2025
by: liko81 | last post by:
I have an Invoice class that must know, directly or indirectly, how to do anything associated with creating, reading, or otherwise processing an invoice to a customer. It is an uber-DAO object that combines the business logic and DAO layers. The invoice it handles can have one to many lines. The most logical implementation is an Invoice object with an array of InvoiceLine objects. We of course want to prevent Joe Programmer from directly...
2
13860
by: Zytan | last post by:
This code works: Type t = myVar.GetType(); System.Reflection.FieldInfo fields = t.GetFields(); foreach (System.Reflection.FieldInfo field in fields) WriteLine(field.Name + " = " + field.GetValue(myVar); But, when one of the fields is an array of string, this will just print:
0
8428
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8335
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8747
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8528
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8627
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6179
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
2752
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1976
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.