Object API

The object API deals with all the operations shared by objects, value types, arrays.

The object API has methods for accessing fields, properties, methods, events, delegates.

There are some advanced uses that are useful to document here dealing with remote fields.

Synopsis

#include <metadata/object.h> typedef struct MonoVTable MonoVTable; typedef struct _MonoThreadsSync MonoThreadsSync; typedef struct { MonoVTable *vtable; MonoThreadsSync *synchronisation; } MonoObject; typedef struct { guint32 length; guint32 lower_bound; } MonoArrayBounds; typedef struct { MonoObject obj; /* bounds is NULL for szarrays */ MonoArrayBounds *bounds; /* total number of elements of the array */ guint32 max_length; /* we use double to ensure proper alignment on platforms that need it */ double vector [MONO_ZERO_LEN_ARRAY]; } MonoArray; @API_IDX@

`MonoObject` is the base definition for all managed objects in the Mono runtime, it represents the System.Object managed type.

All objects that derive from System.Object do have this base definition. Derived objects are declared following the pattern where the parent class is the first field of a structure definition, for example:

typedef struct { MonoObject parent; int my_new_field; } MyNewObject

Core Object Methods

mono_object_new

For example, if you wanted to create an object of type System.Version, you would use a piece of code like this:

MonoClass *version_class; MonoObject *result; /* Get the class from mscorlib */ version_class = mono_class_from_name (mono_get_corlib (), "System", "Version"); /* Create an object of that class */ result = mono_object_new (mono_domain_get (), version_class);

mono_object_new_alloc_specific

mono_object_new_fast

mono_object_new_from_token

mono_object_new_specific

mono_object_clone

mono_object_get_class

mono_object_get_domain

mono_object_get_virtual_method

mono_object_isinst_mbyref

mono_object_isinst

mono_object_unbox

mono_object_castclass_mbyref

mono_object_get_size

mono_object_hash

mono_object_to_string

Value Types

mono_value_box

mono_value_copy

mono_value_copy_array

Array Methods

Use the mono_array_new_* methods to create arrays of a given type.

For example, the following code creates an array with two elements of type System.Byte, and sets the values 0xca and 0xfe on it:

MonoArray *CreateByteArray (MonoDomain *domain)
{
    MonoArray *data;

    data = mono_array_new (domain, mono_get_byte_class (), 2);
    mono_array_set (data, guint8, 0, 0xca);
    mono_array_set (data, guint8, 0, 0xfe);

    return data;
}
	

Creating Arrays

mono_array_new

mono_array_new_full

mono_array_new_specific

mono_array_class_get

mono_array_clone

Using Arrays

Arrays are represented by the `MonoArray` data type and are instances of `MonoObject`. While you can use the `bounds` and `max_length` fields of the type, the actual storage (represented by `vector`) is not very useful. Instead you should use one of the accesor methods described below to fetch or set the values in the array.

When setting values in an array, you should use mono_array_set for setting elements in an array that contains value types, and mono_array_setref for arrays that contain reference types.

The mono_array_get, mono_array_set and mono_array_setref are C macros that wrap the underlying array access.

mono_array_get

mono_array_length

mono_array_set

mono_array_setref

mono_array_addr

mono_array_addr_with_size

mono_array_element_size

Fields

mono_field_from_token

mono_field_get_flags

mono_field_get_name

mono_field_get_parent

mono_field_get_type

mono_field_get_value

mono_field_get_value_object

mono_field_set_value

mono_field_static_get_value

mono_field_static_set_value

mono_field_get_object

Properties

mono_property_get_object

mono_property_get_flags

mono_property_get_get_method

mono_property_get_name

mono_property_get_parent

mono_property_get_set_method

mono_property_get_value

mono_property_set_value

Events

mono_event_get_object

mono_event_get_add_method

mono_event_get_flags

mono_event_get_name

mono_event_get_parent

mono_event_get_raise_method

mono_event_get_remove_method

Remote Fields

mono_load_remote_field

mono_load_remote_field_new

mono_store_remote_field

mono_store_remote_field_new

Delegates

mono_get_delegate_begin_invoke

mono_get_delegate_end_invoke