Re: using reflection to discover a nested structure
- From: Bill Grigg <BillGrigg@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 7 Nov 2006 08:59:02 -0800
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.
- Follow-Ups:
- Re: using reflection to discover a nested structure
- From: Bill Grigg
- Re: using reflection to discover a nested structure
- References:
- Re: using reflection to discover a nested structure
- From: Jon Shemitz
- Re: using reflection to discover a nested structure
- From: Bill Grigg
- Re: using reflection to discover a nested structure
- From: Jon Shemitz
- Re: using reflection to discover a nested structure
- Prev by Date: Re: Creating a strongly typed object from a basic serialized class
- Next by Date: Re: Microsoft Asleep At The Wheel Again: Missing the New Era of CAD
- Previous by thread: Re: using reflection to discover a nested structure
- Next by thread: Re: using reflection to discover a nested structure
- Index(es):
Relevant Pages
|