Getting data from an array of objects
-
I need a way to get the values of fields from an array of classes, passed as an object to a method.
myClass[] mc = new myClass[10];
// set properties, fields, etc.
myMethod(mc);where myMethod is defined as follows.
public void myMethod(object obj) {
Type t = obj.GetType();
if (t.IsArray) {
// how can I get the values of fields
// for each element of the array ?
}
else {
foreach (FieldInfo fi in t.GetFields()) {
Type fieldType = fi.FieldType;
// do something
}
}
}Thank you for any help.
-
I need a way to get the values of fields from an array of classes, passed as an object to a method.
myClass[] mc = new myClass[10];
// set properties, fields, etc.
myMethod(mc);where myMethod is defined as follows.
public void myMethod(object obj) {
Type t = obj.GetType();
if (t.IsArray) {
// how can I get the values of fields
// for each element of the array ?
}
else {
foreach (FieldInfo fi in t.GetFields()) {
Type fieldType = fi.FieldType;
// do something
}
}
}Thank you for any help.
-
Use System.Reflection; MemberInfo[] mi = t[i].GetType().GetMembers(); MemberInfo type gives informtion about members
Thank you, but I have tried that already. It does not compile, returning the error "cannot apply indexing [] to an expression of type 'System.Type' (or something like that) for "t[i]".