473,473 Members | 1,604 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to use reflection to list [DateTime] array values

Hi All,

I am on listing a class's fields with reflection and I have to handle fields
which are array of type DateTime [others also]. I cannot get this to work
and I get an unexpected run-time exception:

//The class to analyze
public class Args
{
public DateTime[] start;
}

public static void Main(string[] args)
{
Args a = new Args();
a.start = new DateTime[2] {DateTime.Now, DateTime.Now};
Console.WriteLine("start[0]:{0}", a.start[0]); //The values are really
there....

FieldInfo[] fis = a.GetType().GetFields(BindingFlags.Public |
BindingFlags.Instance);
for(int i = 0; i < fis.Length; i++)
{
if( fis[i].FieldType.IsArray)
{
object[] vals = (object[]) fis[i].GetValue(a);
//CRASH:Specified cast is not valid
foreach (object o in vals)
{
//...list all the values of the field for each array-member

I looks like I cannot cast a DateTime [or other arrays] to object[]. But the
most ugly problem is, that it was working for a long time and now stops!!!!
I have not made any changes to the code [above], and I am out of
understanding, what's happening.

Any help would be really great!!!!

Best regards,
Manfred Braun

(Private)
Mannheim
Germany

mailto:_m*************@manfbraun.de
(Remove the anti-spam-underscore to mail me!)
Nov 16 '05 #1
2 8179
Manfred,
for(int i = 0; i < fis.Length; i++)
{
if( fis[i].FieldType.IsArray)
{
object[] vals = (object[]) fis[i].GetValue(a);
//CRASH:Specified cast is not valid
foreach (object o in vals)
{
//...list all the values of the field for each array-member


You'll have to do something like

Array vals = (Array)fis[i].GetValue(a);
for ( int j = 0; j < vals.Length; j++ )
// list vals.GetValue(j)

That should work for one-dimensional arrays. It gets a litte more
complicated if you have to support multidimensional arrays too.


Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #2
Hello Mattias,

thanks for the help, it works now. Within my last steps, I converted my
"Args" object's field to be an array;So my previous codes was working, but
just not with arrays .... :-(

Thanks a lot!! But I have to go to my C# book, to understand this ;-)

Best regards,
Manfred Braun

(Private)
Mannheim
Germany

mailto:_m*************@manfbraun.de
(Remove the anti-spam-underscore to mail me!)

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Manfred,
for(int i = 0; i < fis.Length; i++)
{
if( fis[i].FieldType.IsArray)
{
object[] vals = (object[]) fis[i].GetValue(a);
//CRASH:Specified cast is not valid
foreach (object o in vals)
{
//...list all the values of the field for each
array-member
You'll have to do something like

Array vals = (Array)fis[i].GetValue(a);
for ( int j = 0; j < vals.Length; j++ )
// list vals.GetValue(j)

That should work for one-dimensional arrays. It gets a litte more
complicated if you have to support multidimensional arrays too.


Mattias

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

Nov 16 '05 #3

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

Similar topics

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...
4
by: Brian Brane | last post by:
I have properties that wrap DataRow columns as in: public int aNumber { get{ return m_DataRow; } set{ m_DataRow = value; } } If the column happens to contain DBNull, I get a cast exception...
3
by: Manfred Braun | last post by:
Hi All, I try to list the values from an objects array member: FieldInfo fis = a.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance); for(int i = 0; i < fis.Length; i++) {
10
by: Ryan Graham | last post by:
I totally bombed this question in an interview so I'm posting my answer here for comments and suggestions... perhaps (god help me) I'm just not that bright, but this works and seems to be fairly...
2
by: Luis Arvayo | last post by:
Hi, In c#, I need to dynamically create types at runtime that will consist of the following: - inherits from a given interface - will have a constructor with an int argument
18
by: Sean | last post by:
I have been using List(of String) when I could easily be using a string array instead. Is it still considered best practice to use Generic list of string rather then a string array? Thanks
7
by: bonk | last post by:
Ist there a collection that holds its data sorted (i.e. inserts items sorted) AND allows to have duplicate values (by wich is sorted) ? I would like to store elements in that collection wich should...
9
by: Rick | last post by:
I have a large list of objects where each object has a unique (non-overlapping) date range. The list is sorted chronologically. What is the most efficient way to search this list for a single...
1
by: jitupatil2007 | last post by:
hi friends i am reading a csv file using string array and after that to keep a validation check i have added the array to list and using list i am accessing the records correctly but i have a...
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
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...
1
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.