B
bill
All,
I have an unmanaged data structure that I use to pass to an umanaged
DLL written in C. This works great. The structure looks sort of like
this:
unsafe public struct Inputs
{
.....
[StructLayout(LayoutKind.Sequential)]
public struct InputA
{
public fixed double array1[3];
public double dt;
public fixed double array2[3];
}
.....
}
The unmanaged DLL is compiled as part of my Solution so there should be
a bunch of metadata laying around. I now know how to read a value from
a field in the structure thru reflection - more or less like this:
Inputs.InputA inp = new Inputs.InputA();
....
Type t = inp.GetType();
MemberInfo[] m = t.GetMembers();
// Find the index in m[] where the Field "dt" is located (not a
problem) - "9" in this example
Object dt = t.InvokeMember(m[9].Name, BindingFlags.GetField,
null, inp, null);
Wonderfully, dt contains the value from the inp structure. Now the
problem... I want to set the value of "dt" in the inp structure to
something else. I try the following:
Object[] newDT= new object[] { 2.777 };
t.InvokeMember(myMemberInfo[9].Name,
BindingFlags.DeclaredOnly | BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.Static |
BindingFlags.SetField, null, inp, newDT);
The value of "dt" in the inp structure does NOT change, and there is no
error message. Any suggestions greatfully received. Yes, I know that I
can access the structure directly. However, I REALLY DO have a good
reason to want to use reflection to do this.
TIA,
Bill
I have an unmanaged data structure that I use to pass to an umanaged
DLL written in C. This works great. The structure looks sort of like
this:
unsafe public struct Inputs
{
.....
[StructLayout(LayoutKind.Sequential)]
public struct InputA
{
public fixed double array1[3];
public double dt;
public fixed double array2[3];
}
.....
}
The unmanaged DLL is compiled as part of my Solution so there should be
a bunch of metadata laying around. I now know how to read a value from
a field in the structure thru reflection - more or less like this:
Inputs.InputA inp = new Inputs.InputA();
....
Type t = inp.GetType();
MemberInfo[] m = t.GetMembers();
// Find the index in m[] where the Field "dt" is located (not a
problem) - "9" in this example
Object dt = t.InvokeMember(m[9].Name, BindingFlags.GetField,
null, inp, null);
Wonderfully, dt contains the value from the inp structure. Now the
problem... I want to set the value of "dt" in the inp structure to
something else. I try the following:
Object[] newDT= new object[] { 2.777 };
t.InvokeMember(myMemberInfo[9].Name,
BindingFlags.DeclaredOnly | BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.Static |
BindingFlags.SetField, null, inp, newDT);
The value of "dt" in the inp structure does NOT change, and there is no
error message. Any suggestions greatfully received. Yes, I know that I
can access the structure directly. However, I REALLY DO have a good
reason to want to use reflection to do this.
TIA,
Bill