473,705 Members | 5,438 Online
Bytes | Software Development & Data Engineering Community
+ 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(La youtKind.Sequen tial)]
unsafe public struct MyOutputs
{
[StructLayout(La youtKind.Sequen tial)]
unsafe public struct TelemListA
{
public double navTime;
public double navMode;
public Outputs outputs;
}
[StructLayout(La youtKind.Sequen tial)]
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.ToS tring().Equals( "NestedType "), I cannot
figure out how to drill down from that point.
TIA,
Bill

Nov 2 '06 #1
9 2391
Bill Grigg wrote:
Once I get the list of MemberInfo[] and determine that
MemberInfo[i].MemberType.ToS tring().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(Typ e T)
{
foreach (MemberInfo Member in T.GetMembers())
{
if (Member.Declari ngType == T)
Console.WriteLi ne("{0}.{1}", T.FullName, Member.Name);
if (Member is Type)
ListMembers((Ty pe)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(Memb er.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.ToS tring().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(Typ e T)
{
foreach (MemberInfo Member in T.GetMembers())
{
if (Member.Declari ngType == T)
Console.WriteLi ne("{0}.{1}", T.FullName, Member.Name);
if (Member is Type)
ListMembers((Ty pe)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(Memb er.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.SetVa lue(...) 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(Memb er.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.ToS tring().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(Typ e T)
{
foreach (MemberInfo Member in T.GetMembers())
{
if (Member.Declari ngType == T)
Console.WriteLi ne("{0}.{1}", T.FullName, Member.Name);
if (Member is Type)
ListMembers((Ty pe)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(Memb er.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.GetF ield("dt");

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

// Write Member.Field
dtfield.SetValu e(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.SetVa lue(...) and the is no plan old SetValue(...).
System.Reflecti on.

--

..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.GetMembe rs();
Object obj = myType.InvokeMe mber(m[9].Name, BindingFlags.Ge tField,
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.InvokeMe mber(myMemberIn fo[9].Name, BindingFlags.Se tField,
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(Memb er.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.GetF ield("dt");

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

// Write Member.Field
dtfield.SetValu e(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.SetVa lue(...) and the is no plan old SetValue(...).

System.Reflecti on.

--

..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(La youtKind.Sequen tial)]
unsafe public struct Inputs
{
[StructLayout(La youtKind.Sequen tial)]
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.GetMembe rs();
Object obj = myType.InvokeMe mber(m[9].Name, BindingFlags.Ge tField,
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.InvokeMe mber(myMemberIn fo[9].Name, BindingFlags.Se tField,
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(Memb er.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.GetF ield("dt");

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

// Write Member.Field
dtfield.SetValu e(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.SetVa lue(...) and the is no plan old SetValue(...).
System.Reflecti on.

--

..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(La youtKind.Sequen tial)]
unsafe public struct Inputs
{
[StructLayout(La youtKind.Sequen tial)]
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.GetMembe rs();
Object obj = myType.InvokeMe mber(m[9].Name, BindingFlags.Ge tField,
null, inputs, null);
Object[] objA = new object[] { 2.777 };
o = myType.InvokeMe mber(myMemberIn fo[9].Name, BindingFlags.Se tField,
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(La youtKind.Sequen tial)]
unsafe public struct Inputs
{
[StructLayout(La youtKind.Sequen tial)]
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.GetMembe rs();
Object obj = myType.InvokeMe mber(m[9].Name, BindingFlags.Ge tField,
null, inputs, null);
Object[] objA = new object[] { 2.777 };
o = myType.InvokeMe mber(myMemberIn fo[9].Name, BindingFlags.Se tField,
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
16903
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. This works fine if all of the properties are at the top (root level) of the model but I'd like to keep them in nested classes to organize them better. So, for example, part of my data model looks like this (simplified) : public class MainClass
2
2984
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 types of items in the very hierarchical data model: - Classes (and nested classes) - Collections - Properties I've successfully written the Reflection code to handle any combination of classes and properties but I'm confused about what to do...
2
2007
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 information would then be logged to a database along with some other information about the user. Thanks in advance. Mark
13
12382
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
288
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 in my control and then use reflection to get all the methods on it to display some sort of intellisense thing etc... What I want to be able to do however is discover loaded public classes at runtime - which may or may not be forms...
5
1860
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 author of an application, I already know the metadata about my classes - I wrote them. And even if I did not - Let's say I buy a 3rd party class lib, it will be documented for me with that lib. If I am part of a dev team, the same is true - If...
3
4097
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 the Properties into Fields when it comes out of the Web Service). I have this at the moment: Private _aDataSource As Object 'Person class
2
4887
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 that MemberInfo.MemberType.ToString().Equals("NestedType"), I cannot figure out how to drill down from that point. TIA,
9
1942
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 name of that property? So: string name = GetPropertyName(int.MaxValue); // returns “MaxValue” string name = GetPropertyName(DateTime.Now); // returns “Now” (Something similar exists to...
0
8690
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9139
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8979
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7895
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 projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4704
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3138
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2491
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2083
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.