473,395 Members | 1,532 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,395 software developers and data experts.

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.FieldType shows "MyArray[]", this is correct for me. If I try
with

arrayField.SetValue(records.ToArray(), instanceOfMyClass);

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 1991
Hi Wernfried,

Before digging more deeply into this.

Are you sure that the invalid cast is because of the arrays
arrayField.SetValue(records.ToArray(), instanceOfMyClass);
AFIK in FiledInfo.SetValue first comes the object which field you set and
then the value of the field. So I believe you should switch the paramers

arrayField.SetValue(instanceOfMyClass, records.ToArray());

Try that and tell if the probles is still there

--
HTH
Stoitcho Goutsev (100) [C# MVP]
"Wernfried Schwenkner" <we*************************@sesa.de> wrote in
message news:ol***********@news.bln.sesa.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.FieldType shows "MyArray[]", this is correct for me. If I try
with

arrayField.SetValue(records.ToArray(), instanceOfMyClass);

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.SetValue(records.ToArray(), instanceOfMyClass);
AFIK in FiledInfo.SetValue first comes the object which field you set and
then the value of the field. So I believe you should switch the paramers

arrayField.SetValue(instanceOfMyClass, records.ToArray());

Try that and tell if the probles is still there

--
HTH
Stoitcho Goutsev (100) [C# MVP]
"Wernfried Schwenkner" <we*************************@sesa.de> wrote in
message news:ol***********@news.bln.sesa.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.FieldType shows "MyArray[]", this is correct for me. If I try
with

arrayField.SetValue(records.ToArray(), instanceOfMyClass);

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.SetValue(instanceOfMyClass, 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.SetValue(instanceOfMyClass, 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
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...
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...
3
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...
12
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...
1
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...
2
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....
1
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...
0
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...
2
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 + " = " +...
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: 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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
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...
0
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...

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.