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!)
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!)