Hi Jon
Thanks for the offer. I did dabble with Reflector yesterday, which did show
how to create the object using:
[StructLayout(LayoutKind.Explicit, Size=0x18, Pack=8), DebugInfo(new byte[] {
0x69, 0x69, 110, 0x74, 0x31, 0, 0x20, 0, 0, 0, 4, 0, 0, 0, 0x10, 0,
0, 0, 4, 0, 0, 0, 0x69, 0x69, 110, 0x74, 50, 0, 0x20, 0, 0, 0,
4, 0, 0, 0, 20, 0, 0, 0, 4, 0, 0, 0, 100, 100, 0x6f, 0x75,
0x62, 0x6c, 0x65, 0x31, 0, 0x20, 0, 0, 0, 11, 0, 0, 0, 0, 0, 0,
0, 8, 0, 0, 0, 100, 100, 0x6f, 0x75, 0x62, 0x6c, 0x65, 50, 0, 0x20, 0,
0, 0, 11, 0, 0, 0, 8, 0, 0, 0, 8, 0, 0, 0, 0, 0
})]
public struct DNVSRISKMDECORE!STRUCT1
{
// Fields
[FieldOffset(0)]
public double ddouble1;
[FieldOffset(8)]
public double ddouble2;
[FieldOffset(0x10)]
public int iint1;
[FieldOffset(20)]
public int iint2;
}
The good news for me is that because of our application design, all the
methods and objects (lots of) are stored on a data dictionary and they are
built using the late invocation approach anyway.
Thanks again.
Best regards
Marek
Jon Skeet said:
Marek said:
Thanks for all your replies. I was worried that you were going to say that.
Our requirement is to call a method that uses this object so I can get away
with creating and using the object as follows:
Type struct1Type = assembly.GetType("DNVSRISKMDECORE!STRUCT1");
object struct1Instance = Activator.CreateInstance(struct1Type);
struct1Type.InvokeMember("iint1", BindingFlags.SetField, null,
struct1Instance, new object[] { -1 });
struct1Type.InvokeMember("iint2", BindingFlags.SetField, null,
struct1Instance, new object[] { -2 });
struct1Type.InvokeMember("ddouble1", BindingFlags.SetField,
null, struct1Instance, new object[] { -10.0 });
struct1Type.InvokeMember("ddouble2", BindingFlags.SetField,
null, struct1Instance, new object[] { -20.0 });
Thanks for your responses.
Do you only have to do this once? If so, that's a reasonable workaround
(horrible as it is that you have to do it). If you need to use it more
often, I'm happy to help you with the alternative idea about a
surrogate structure.