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 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
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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....
|
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...
|
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...
|
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 + " = " +...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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: 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...
|
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...
| |