How do I get Array elements from Reflection C#

A

ashraftm

How do I get Array elements value from Reflection..

Here is the C# code..

I have 2 struc

Public struct Address {
public string city;
}

public struct Person {
public Address[] Experience;
}


Person p = new Person();
p.Experience = new Address[2];
p.Experience[0] = "Los Angeles";
p.Experience[1] = "NewYork";


Type personInfo = p.GetType();
FieldInfo[] fieldInfo = personInfo.GetFields();

foreach(FieldInfo info in fieldInfo) {
if (info.FieldType.IsArray) {
Object Temp = (Object)info.GetValue(sender);
int len = ((Array)Temp).Length;

// Error here. Error is cannot convert Address type to Object type
Object[] a = (Object[])Temp;
// If I do like "Address[] a = (Address[])Temp;" then
it works...But this class is a
helper class for serialization like JSON. So Object can be any
type.

for (int i = 0; i < len; i++)
result = a.City;
}
}

Anyone have any better idea.
All I need is to get object field name and its value.

thanks in advance.

achu.
 
J

Jon Skeet [C# MVP]

How do I get Array elements value from Reflection..

You can cast to Array and then use Array.GetValue to get a value.

The reason your cast to object[] didn't work is that Person is a value
type, and array covariance is only in terms of reference types (so a
string[] can be cast to an object[], but an int[] can't).
 
A

ashraftm

Hi,

Thats what I was looking for. Thank you very much for the input both
Jon and Mattias.
Quick question.
How do I find FieldType is a custom class or struct other than system
generic types. I could use "System.String", "System.Int32" etc.. Any
shot cuts ? Temp I use dirty way. Type.namespace != "System"

thanks
regards,
achu.
 
J

Jon Skeet [C# MVP]

Thats what I was looking for. Thank you very much for the input both
Jon and Mattias.
Quick question.
How do I find FieldType is a custom class or struct other than system
generic types. I could use "System.String", "System.Int32" etc.. Any
shot cuts ? Temp I use dirty way. Type.namespace != "System"

I'm not entirely sure what you mean. If you give a very precise
definition of exactly what you want the criteria to be, I suspect
you'll find that translates into code fairly naturally.
 
A

ashraftm

hi Jon,

How do I differentiate built in C# data types (Int32, String, DateTime
etc..) and user defind data types (Eg. "Address Struct") using
reflection? My Idea is to get all custom defind struct and class.

thanks,
Achu.
 
Joined
Oct 6, 2009
Messages
2
Reaction score
0
Similar problem like achu

Hello,

i have a similar problem look at the following code:

public class FieldInfoClass
{
public int[] myIntArray = new int[0];

public static void Main()
{
object o = new FieldInfoClass();

Type myType = typeof(FieldInfoClass);

// Get the type and fields of FieldInfoClass.
FieldInfo[] myFieldInfo = myType.GetFields();

//There is only one Field in the FieldInfoClass!
FieldInfo fi = myFieldInfo[0];

Type type1 = fi.FieldType;
Debug.Print("type1: " + type1.ToString());
//Output:
//type1: System.Array

//But I want the type 'Int32'!!!

object p = fi.GetValue(o);
Type type2 = p.GetType();
Debug.Print("type2: " + type2.ToString());
//Output:
//type2: System.Int32[]

//But I want the type 'Int32'!!!

//My problem is that FieldInfoClass is a class where the members have no value at this time!
//They should get a value and because of this I want to know the type of the array.
}
}

Has someone a idea how I can get out the type of the array?
 

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