using reflection to discover a nested structure

G

Guest

All,

Can anyone supply an example or reference to an example of using
reflection to determine the data types and array lengths contained in a
nested stucture in C#? Actually, it is a structure that I use to communicate
to some unmanaged code in a DLL written in C. It is not complicated, but will
change and I would like to be able to sequentially access it without
explicitly referring to each and every element. Here is the structure:

[StructLayout(LayoutKind.Sequential)]
unsafe public struct MyOutputs
{
[StructLayout(LayoutKind.Sequential)]
unsafe public struct TelemListA
{
public double navTime;
public double navMode;
public Outputs outputs;
}
[StructLayout(LayoutKind.Sequential)]
unsafe public struct Outputs
{
public fixed double Q_isa[4];
public fixed double dV[3];
public fixed double dT[3];
public fixed double Q_mid[4];
}
}

Once I get the list of MemberInfo[] and determine that
MemberInfo.MemberType.ToString().Equals("NestedType"), I cannot
figure out how to drill down from that point.


TIA,


Bill
 
J

Jon Shemitz

Bill said:
Once I get the list of MemberInfo[] and determine that
MemberInfo.MemberType.ToString().Equals("NestedType"), I cannot
figure out how to drill down from that point.


In that case, the MemberInfo is a Type. For example,

private static void ListMembers(Type T)
{
foreach (MemberInfo Member in T.GetMembers())
{
if (Member.DeclaringType == T)
Console.WriteLine("{0}.{1}", T.FullName, Member.Name);
if (Member is Type)
ListMembers((Type)Member);
}
}
 
G

Guest

Jon,

Thanks, that helped a lot. Now I have (I think) just one more question:

Given that I have drilled down and found the fields in an instantiated
object (well, a stucture of doubles actually), I would like to either read
the values of the fields, or store something into them. When I found
field.SetValue(...) I thought I was all set except that it is
field.SetValue(obj x, obj value). Now I cannot figure out what's the field
and what's the object. I did the following:

Type t = Member.GetType();
FieldInfo field = t.GetField(Member.Name);
field.SetValue(now what do I do???);

TIA

Bill

Jon Shemitz said:
Bill said:
Once I get the list of MemberInfo[] and determine that
MemberInfo.MemberType.ToString().Equals("NestedType"), I cannot
figure out how to drill down from that point.


In that case, the MemberInfo is a Type. For example,

private static void ListMembers(Type T)
{
foreach (MemberInfo Member in T.GetMembers())
{
if (Member.DeclaringType == T)
Console.WriteLine("{0}.{1}", T.FullName, Member.Name);
if (Member is Type)
ListMembers((Type)Member);
}
}
 
J

Jon Shemitz

Bill said:
Given that I have drilled down and found the fields in an instantiated
object (well, a stucture of doubles actually), I would like to either read
the values of the fields, or store something into them. When I found
field.SetValue(...) I thought I was all set except that it is
field.SetValue(obj x, obj value). Now I cannot figure out what's the field
and what's the object. I did the following:

Hmm, the docs seem pretty clear, to me:

public void SetValue (
Object obj,
Object value
)

obj
The object whose field value will be set.

value
The value to assign to the field.

That is, it's modeled on assignment. Field.SetValue(Instance,
NewValue) sets Instance.Field to NewValue.
 
G

Guest

I guess I am having another mental block. WRT the following snippet:


Type t = Member.GetType();
FieldInfo field = t.GetField(Member.Name);
field.SetValue(now what do I do???);

1. As far as I can figure out "field" is the field in my data structure. To
be more specific let's say the structure is:

struct Inputs {
double arr1[3];
double dt;
double arr2[3];
};

and I have located "dt". How do I read/write "dt"?

2. The only "SetValue()" that I can find is the one belonging to "field". So
if I write:

field.SetValue(???, 1.2345); // What is ??? supposed to be?

3. Unless I am missing a namespace reference there is no
FieldInfo.SetValue(...) and the is no plan old SetValue(...).

Bill Grigg said:
Jon,

Thanks, that helped a lot. Now I have (I think) just one more question:

Given that I have drilled down and found the fields in an instantiated
object (well, a stucture of doubles actually), I would like to either read
the values of the fields, or store something into them. When I found
field.SetValue(...) I thought I was all set except that it is
field.SetValue(obj x, obj value). Now I cannot figure out what's the field
and what's the object. I did the following:

Type t = Member.GetType();
FieldInfo field = t.GetField(Member.Name);
field.SetValue(now what do I do???);

TIA

Bill

Jon Shemitz said:
Bill said:
Once I get the list of MemberInfo[] and determine that
MemberInfo.MemberType.ToString().Equals("NestedType"), I cannot
figure out how to drill down from that point.


In that case, the MemberInfo is a Type. For example,

private static void ListMembers(Type T)
{
foreach (MemberInfo Member in T.GetMembers())
{
if (Member.DeclaringType == T)
Console.WriteLine("{0}.{1}", T.FullName, Member.Name);
if (Member is Type)
ListMembers((Type)Member);
}
}
 
J

Jon Shemitz

Bill said:
I guess I am having another mental block. WRT the following snippet:

Type t = Member.GetType();
FieldInfo field = t.GetField(Member.Name);
field.SetValue(now what do I do???);

field.SetValue(Member, 1.2345);
1. As far as I can figure out "field" is the field in my data structure. To
be more specific let's say the structure is:

struct Inputs {
double arr1[3];
double dt;
double arr2[3];
};

and I have located "dt". How do I read/write "dt"?

Type MemberType = Member.GetType();
FieldInfo dtField = MemberType.GetField("dt");

// Read Member.Field
double CurrentValue = (double) dtField.GetValue(Member);

// Write Member.Field
dtfield.SetValue(Member, 1.2345);
2. The only "SetValue()" that I can find is the one belonging to "field". So
if I write:

field.SetValue(???, 1.2345); // What is ??? supposed to be?

An object reference.
3. Unless I am missing a namespace reference there is no
FieldInfo.SetValue(...) and the is no plan old SetValue(...).

System.Reflection.
 
G

Guest

Jon,

I appreciate the help. I think the fundamental problem was that I was not
working with an instance of the object (i.e. my data structure). In any case
the following works ("inputs" is an instance of the data structure):

Type myType = inputs.GetType();
MemberInfo[] m= myType.GetMembers();
Object obj = myType.InvokeMember(m[9].Name, BindingFlags.GetField,
null, inputs, null); // I use m[9] because I cheated and found tolut hat is
where "dt" is

The obj object contains the value of "dt" mention earlier. Now I cannot
figure out how to set the value of "dt". If I write:

Object[] objA = new object[] { 2.777 };
o = myType.InvokeMember(myMemberInfo[9].Name, BindingFlags.SetField,
null, intIMUIn, objA);

the field "dt" remains unchanged.

By the way, the reason that I am doing this is that I will be testing a
series of functions with input parameters that are basically structures of
nested "doubles". I will be receiving input vectors for these functions that
have these doubles arranged in the order that they appear in the structures.
I do not want to have to reference each element of each structure
"explicitly" because there will be literally hundreds of input "doubles" in
some cases. All of the functions are written in C (unmanaged) and reside in a
DLL that fortunately I can build into my Solution.

One other issue - most of the elements of the structures are arrays, so I
need to find out how to reference each element of a nested array through
reflection.

Hope this is not too confusing...

Thanks,

Bill


Jon Shemitz said:
Bill said:
I guess I am having another mental block. WRT the following snippet:

Type t = Member.GetType();
FieldInfo field = t.GetField(Member.Name);
field.SetValue(now what do I do???);

field.SetValue(Member, 1.2345);
1. As far as I can figure out "field" is the field in my data structure. To
be more specific let's say the structure is:

struct Inputs {
double arr1[3];
double dt;
double arr2[3];
};

and I have located "dt". How do I read/write "dt"?

Type MemberType = Member.GetType();
FieldInfo dtField = MemberType.GetField("dt");

// Read Member.Field
double CurrentValue = (double) dtField.GetValue(Member);

// Write Member.Field
dtfield.SetValue(Member, 1.2345);
2. The only "SetValue()" that I can find is the one belonging to "field". So
if I write:

field.SetValue(???, 1.2345); // What is ??? supposed to be?

An object reference.
3. Unless I am missing a namespace reference there is no
FieldInfo.SetValue(...) and the is no plan old SetValue(...).

System.Reflection.
 
G

Guest

Jon,

I just discovered that it works if it is a managed data structure!!! Do I
have a chance of doing this if the structure is declared thusly:

[StructLayout(LayoutKind.Sequential)]
unsafe public struct Inputs
{
[StructLayout(LayoutKind.Sequential)]
unsafe public struct MyInputs
{
public fixed double array1[3];
public fixed double array2[3];
public double dt;
public fixed double array3[3];
}
}


Bill Grigg said:
Jon,

I appreciate the help. I think the fundamental problem was that I was not
working with an instance of the object (i.e. my data structure). In any case
the following works ("inputs" is an instance of the data structure):

Type myType = inputs.GetType();
MemberInfo[] m= myType.GetMembers();
Object obj = myType.InvokeMember(m[9].Name, BindingFlags.GetField,
null, inputs, null); // I use m[9] because I cheated and found tolut hat is
where "dt" is

The obj object contains the value of "dt" mention earlier. Now I cannot
figure out how to set the value of "dt". If I write:

Object[] objA = new object[] { 2.777 };
o = myType.InvokeMember(myMemberInfo[9].Name, BindingFlags.SetField,
null, intIMUIn, objA);

the field "dt" remains unchanged.

By the way, the reason that I am doing this is that I will be testing a
series of functions with input parameters that are basically structures of
nested "doubles". I will be receiving input vectors for these functions that
have these doubles arranged in the order that they appear in the structures.
I do not want to have to reference each element of each structure
"explicitly" because there will be literally hundreds of input "doubles" in
some cases. All of the functions are written in C (unmanaged) and reside in a
DLL that fortunately I can build into my Solution.

One other issue - most of the elements of the structures are arrays, so I
need to find out how to reference each element of a nested array through
reflection.

Hope this is not too confusing...

Thanks,

Bill


Jon Shemitz said:
Bill said:
I guess I am having another mental block. WRT the following snippet:

Type t = Member.GetType();
FieldInfo field = t.GetField(Member.Name);
field.SetValue(now what do I do???);

field.SetValue(Member, 1.2345);
1. As far as I can figure out "field" is the field in my data structure. To
be more specific let's say the structure is:

struct Inputs {
double arr1[3];
double dt;
double arr2[3];
};

and I have located "dt". How do I read/write "dt"?

Type MemberType = Member.GetType();
FieldInfo dtField = MemberType.GetField("dt");

// Read Member.Field
double CurrentValue = (double) dtField.GetValue(Member);

// Write Member.Field
dtfield.SetValue(Member, 1.2345);
2. The only "SetValue()" that I can find is the one belonging to "field". So
if I write:

field.SetValue(???, 1.2345); // What is ??? supposed to be?

An object reference.
3. Unless I am missing a namespace reference there is no
FieldInfo.SetValue(...) and the is no plan old SetValue(...).

System.Reflection.
 
J

Jon Shemitz

Bill said:
I just discovered that it works if it is a managed data structure!!! Do I
have a chance of doing this if the structure is declared thusly:

[StructLayout(LayoutKind.Sequential)]
unsafe public struct Inputs
{
[StructLayout(LayoutKind.Sequential)]
unsafe public struct MyInputs
{
public fixed double array1[3];
public fixed double array2[3];
public double dt;
public fixed double array3[3];
}
}
Type myType = inputs.GetType();
MemberInfo[] m= myType.GetMembers();
Object obj = myType.InvokeMember(m[9].Name, BindingFlags.GetField,
null, inputs, null);
Object[] objA = new object[] { 2.777 };
o = myType.InvokeMember(myMemberInfo[9].Name, BindingFlags.SetField,
null, intIMUIn, objA);

the field "dt" remains unchanged.

I'm not sure what's going on, and don't currently have the time to run
some experiments. (Maybe later.)

I don't think I'd be using InvokeMember, here: I'd be more inclined to
use the lower level Reflection primitives, like GetField and SetValue.
It's probably more efficient (certainly there are no object arrays
involved) and you may have more control.

On the InvokeMember front, though, I guess I'd investigate the use of
a Binder object (instead of a null 4th parameter). I don't know why
your code works on safe structs but not unsafe structs - there may be
some name hashing going on, and the Binder object may be able to make
some appropriate decisions.

Again, I'll have to get back to you after I have some time for a few
experiments.
 
G

Guest

Jon,

I found the answer. I needed to "box" the input structure and hence make it
"managed".

Bill

Jon Shemitz said:
Bill said:
I just discovered that it works if it is a managed data structure!!! Do I
have a chance of doing this if the structure is declared thusly:

[StructLayout(LayoutKind.Sequential)]
unsafe public struct Inputs
{
[StructLayout(LayoutKind.Sequential)]
unsafe public struct MyInputs
{
public fixed double array1[3];
public fixed double array2[3];
public double dt;
public fixed double array3[3];
}
}
Type myType = inputs.GetType();
MemberInfo[] m= myType.GetMembers();
Object obj = myType.InvokeMember(m[9].Name, BindingFlags.GetField,
null, inputs, null);
Object[] objA = new object[] { 2.777 };
o = myType.InvokeMember(myMemberInfo[9].Name, BindingFlags.SetField,
null, intIMUIn, objA);

the field "dt" remains unchanged.

I'm not sure what's going on, and don't currently have the time to run
some experiments. (Maybe later.)

I don't think I'd be using InvokeMember, here: I'd be more inclined to
use the lower level Reflection primitives, like GetField and SetValue.
It's probably more efficient (certainly there are no object arrays
involved) and you may have more control.

On the InvokeMember front, though, I guess I'd investigate the use of
a Binder object (instead of a null 4th parameter). I don't know why
your code works on safe structs but not unsafe structs - there may be
some name hashing going on, and the Binder object may be able to make
some appropriate decisions.

Again, I'll have to get back to you after I have some time for a few
experiments.
 

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