Re: using reflection to discover a nested structure

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



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.

.



Relevant Pages

  • Re: using reflection to discover a nested structure
    ... Object obj = myType.InvokeMember(m.Name, BindingFlags.GetField, ... I do not want to have to reference each element of each structure ... need to find out how to reference each element of a nested array through ... FieldInfo field = t.GetField; ...
    (microsoft.public.dotnet.general)
  • Re: open() behavior under heavy disk load
    ... effects which may be created because of race conditions ... recently occured when a refence-counted data structure was supposed to ... represented while there was still an active transmission for this ... reference to the first data structure, too, because replies could come ...
    (comp.os.linux.development.system)
  • Re: Dict sharing vs. duplication
    ... data structure that was treated as a first class object - instead, ... What some people would like is reference semantics. ... You could simply use [dict reference] (for ... allow you to avoid upvar in most situations. ...
    (comp.lang.tcl)
  • Re: Dict sharing vs. duplication
    ... data structure that was treated as a first class object - instead, ... Its a first class object. ... What some people would like is reference semantics. ... But here comes one of my three gripes with s. ...
    (comp.lang.tcl)
  • Re: what is the use of weakref?
    ... say you had a word processing application with a complex data ... structure for each paragraph style that was used in a document. ... style of another paragraph you want to share the same data structure rather ... paragraph hold a strong reference to the required data structure, ...
    (comp.lang.python)