[Mono-devel-list] [Embedding Mono] Accessing Fields

Joe Ante joe at uti.is
Wed Aug 4 04:21:54 EDT 2004

> Hi,
> I'm trying to access a field of a C# struct (from C), but i don't get the
> expected result.
> My programm should set i to 11 and then write out this value by calling
> "Write()".
> If T is a class this works, but if T is a struct I dont't get "11".
> By calling mono_field_set with an address reduced by 8 (size of a
> MonoObject?) it works
> (field offset of "i" is 8, using class and using struct)
> Reducing the address by 8 is not very clean,
> so, can anyone help me and tell me what i've done wrong?
I guess that its a bug in mono or mono_field_set_value is just not supposed
to be called on structs.
But if you are using structs anyway it is possibly simpler to just have the
same struct on the C side and use mono_value_box.

[StructLayout (LayoutKind.Sequential)] on the C# side.

struct Cstruct {  int i; };
Cstruct a;
A.i = 10;
MonoObject* o = mono_value_box (domain, klass, &a);

Joachim Ante

More information about the Mono-devel-list mailing list