473,472 Members | 1,882 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

using reflection to discover a nested structure

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[i].MemberType.ToString().Equals("NestedType"), I cannot
figure out how to drill down from that point.
TIA,
Bill

Nov 2 '06 #1
9 2349
Bill Grigg wrote:
Once I get the list of MemberInfo[] and determine that
MemberInfo[i].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);
}
}

--

..NET 2.0 for Delphi Programmers www.midnightbeach.com/.net
Delphi skills make .NET easy to learn Great reviews & good sales.
Nov 2 '06 #2
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" wrote:
Bill Grigg wrote:
Once I get the list of MemberInfo[] and determine that
MemberInfo[i].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);
}
}

--

..NET 2.0 for Delphi Programmers www.midnightbeach.com/.net
Delphi skills make .NET easy to learn Great reviews & good sales.
Nov 6 '06 #3
Bill Grigg wrote:
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.

--

..NET 2.0 for Delphi Programmers www.midnightbeach.com/.net
Delphi skills make .NET easy to learn Great reviews & good sales.
Nov 6 '06 #4
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" wrote:
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" wrote:
Bill Grigg wrote:
Once I get the list of MemberInfo[] and determine that
MemberInfo[i].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);
}
}

--

..NET 2.0 for Delphi Programmers www.midnightbeach.com/.net
Delphi skills make .NET easy to learn Great reviews & good sales.
Nov 6 '06 #5
Bill Grigg wrote:
>
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.

--

..NET 2.0 for Delphi Programmers www.midnightbeach.com/.net
Delphi skills make .NET easy to learn Great reviews & good sales.
Nov 6 '06 #6
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" wrote:
Bill Grigg wrote:

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.

--

..NET 2.0 for Delphi Programmers www.midnightbeach.com/.net
Delphi skills make .NET easy to learn Great reviews & good sales.
Nov 7 '06 #7
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" wrote:
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" wrote:
Bill Grigg wrote:
>
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.

--

..NET 2.0 for Delphi Programmers www.midnightbeach.com/.net
Delphi skills make .NET easy to learn Great reviews & good sales.
Nov 7 '06 #8
Bill Grigg wrote:
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.
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.
Again, I'll have to get back to you after I have some time for a few
experiments.

--

..NET 2.0 for Delphi Programmers www.midnightbeach.com/.net
Delphi skills make .NET easy to learn Great reviews & good sales.
Nov 8 '06 #9
Jon,

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

Bill

"Jon Shemitz" wrote:
Bill Grigg wrote:
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.
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.

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

--

..NET 2.0 for Delphi Programmers www.midnightbeach.com/.net
Delphi skills make .NET easy to learn Great reviews & good sales.
Nov 9 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Robert W. | last post by:
I've almost completed building a Model-View-Controller but have run into a snag. When an event is fired on a form control I want to automatically updated the "connnected" property in the Model. ...
2
by: Robert W. | last post by:
I'm trying to write a utility that will use Reflection to examine any data model I pass it and correctly map out this model into a tree structure. When I say "any" , in fact there will only be 3...
2
by: Mark | last post by:
Am I out of my mind if I use Reflection everytime someone logs into our site to get and track the current Major/Minor/Build/Revision version that the person is viewing our site through? This...
13
by: Don | last post by:
How do I get an Enum's type using only the Enum name? e.g. Dim enumType as System.Type Dim enumName as String = "MyEnum" enumType = ???(enumName)
0
by: StuartJSmith | last post by:
Hi, I am writing a control whereby I wish to be able to run csharp commands dynamically at runtime - this is easy when it is in the context of say, one form. I can have a reference to that form...
5
by: heddy | last post by:
I understand that reflection allows me to discover the metadata of a class at runtime (properties, methods etc). What I don't understand is where this is useful. For example: If I am the sole...
3
by: Steve Amey | last post by:
Hi all I am using reflection to read the values of properties from a class. The class is returned from a Web Service so I have to access the class using FieldInfo (Using VS 2003 which converts...
2
by: bill | last post by:
All, Can anyone supply an example or reference to an example of using reflection to determine the data types contained in a nested stucture in C#? Once I get the list of MemberInfo and determine...
9
by: =?Utf-8?B?VmljdG9y?= | last post by:
Is it a way to discover, at the run time, the name of a property of an object? In other words is it possible to create a method GetPropertyName, that takes a property of an object and returns the...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.