How to use reflection to list [DateTime] array values

M

Manfred Braun

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.FieldType.IsArray)
{
object[] vals = (object[]) fis.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:[email protected]
(Remove the anti-spam-underscore to mail me!)
 
M

Mattias Sjögren

Manfred,
for(int i = 0; i < fis.Length; i++)
{
if( fis.FieldType.IsArray)
{
object[] vals = (object[]) fis.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.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
 
M

Manfred Braun

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:[email protected]
(Remove the anti-spam-underscore to mail me!)

Mattias Sjögren said:
Manfred,
for(int i = 0; i < fis.Length; i++)
{
if( fis.FieldType.IsArray)
{
object[] vals = (object[]) fis.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.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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top