Class Operations
The operations on
MonoClass*
allow you to
query a number of properties of a .NET class from the C API.
This page contains information
for
how to obtain a MonoClass*,
how to
probe for different
features of a MonoClass* and APIs
to
access class members and
access fields. To access
methods, see the
method
documentation API.
Getting a MonoClass
You typically would obtain a
MonoClass*
pointer by using a combination of the
MonoImage*
where the type is located, the namespace and name using the
mono_class_from_name
or
the
mono_class_from_name_case
APIs or by using
the
mono_class_from_typeref
or
mono_class_from_typeref_checked
methods.
For low-level access, you can get the
MonoClass
*
from an image and an ECMA type token relative to
the
MonoImage*
by using
mono_class_get.
mono_class_get
Syntax
MonoClass*
mono_class_get (MonoImage *image, guint32 type_token)
Parameters
image | image where the class token will be looked up. |
type_token | a type token from the image |
Return value
the MonoClass
with the given type_token on the image
mono_class_from_mono_type
Syntax
MonoClass*
mono_class_from_mono_type (MonoType *type)
Parameters
type | describes the type to return |
Return value
a MonoClass
for the specified MonoType
, the value is never NULL
.
mono_class_from_name
Syntax
MonoClass*
mono_class_from_name (MonoImage *image, const char* name_space, const char *name)
Parameters
image | The MonoImage where the type is looked up in |
name_space | the type namespace |
name | the type short name. |
Description
Obtains a
MonoClass
with a given namespace and a given name which
is located in the given
MonoImage
.
To reference nested classes, use the "/" character as a separator.
For example use
"Foo/Bar"
to reference the class
Bar
that is nested
inside
Foo
, like this: "class Foo { class Bar {} }".
mono_class_from_name_case
Deprecated: use the mono_class_from_name_case_checked variant instead.
Syntax
MonoClass*
mono_class_from_name_case (MonoImage *image, const char* name_space, const char *name)
Parameters
image | The MonoImage where the type is looked up in |
name_space | the type namespace |
name | the type short name. |
Description
Obtains a
MonoClass
with a given namespace and a given name which
is located in the given
MonoImage
. The namespace and name
lookups are case insensitive.
mono_class_from_typeref
Syntax
MonoClass*
mono_class_from_typeref (MonoImage *image, guint32 type_token)
Parameters
image | a MonoImage |
type_token | a TypeRef token |
Return value
The MonoClass*
representing the typeref token, or NULL
if it could
not be loaded.
Description
Creates the
MonoClass*
structure representing the type defined by
the typeref token valid inside
image.
mono_class_from_typeref_checked
Syntax
MonoClass*
mono_class_from_typeref_checked (MonoImage *image, guint32 type_token, MonoError *error)
Parameters
image | a MonoImage |
type_token | a TypeRef token |
error | error return code, if any. |
Return value
The MonoClass*
representing the typeref token, NULL
if it could
not be loaded with the error value filled with the information about the
error.
Description
Creates the
MonoClass*
structure representing the type defined by
the typeref token valid inside
image.
mono_class_from_generic_parameter
Syntax
MonoClass*
mono_class_from_generic_parameter (MonoGenericParam *param, MonoImage *arg2 G_GNUC_UNUSED, gboolean arg3 G_GNUC_UNUSED)
Parameters
param | Parameter to find/construct a class for. |
arg2 | Is ignored. |
arg3 | Is ignored. |
Working with a MonoClass
mono_class_array_element_size
Syntax
gint32
mono_class_array_element_size (MonoClass *klass)
Parameters
Return value
The number of bytes an element of type klass uses when stored into an array.
Description
mono_class_data_size
Syntax
gint32
mono_class_data_size (MonoClass *klass)
Parameters
Return value
The size of the static class data
Description
mono_class_enum_basetype
Syntax
MonoType*
mono_class_enum_basetype (MonoClass *klass)
Parameters
klass | the MonoClass to act on |
Return value
The underlying type representation for an enumeration.
Description
Use this function to get the underlying type for an enumeration value.
mono_class_get_byref_type
Syntax
MonoType*
mono_class_get_byref_type (MonoClass *klass)
Parameters
klass | the MonoClass to act on |
Description
mono_class_get_element_class
Syntax
MonoClass*
mono_class_get_element_class (MonoClass *klass)
Parameters
klass | the MonoClass to act on |
Return value
The element class of an array.
Description
Use this function to get the element class of an array.
mono_class_get_event_token
Syntax
guint32
mono_class_get_event_token (MonoEvent *event)
mono_class_get_field
Syntax
MonoClassField*
mono_class_get_field (MonoClass *klass, guint32 field_token)
Parameters
class | the class to lookup the field. |
field_token | the field token |
Return value
A MonoClassField
representing the type and offset of
the field, or a NULL
value if the field does not belong to this
class.
Description
mono_class_get_field_token
Syntax
guint32
mono_class_get_field_token (MonoClassField *field)
Parameters
field | the field we need the token of |
Return value
The token representing the field in the image it was loaded from.
Description
Get the token of a field. Note that the tokesn is only valid for the image
the field was loaded from. Don't use this function for fields in dynamic types.
mono_class_get_flags
Syntax
guint32
mono_class_get_flags (MonoClass *klass)
Parameters
klass | the MonoClass to act on |
Return value
the TypeAttributes
flags of klass.
See the TYPE_ATTRIBUTE_*
definitions in tabledefs.h
for the different values.
mono_class_get_full
Deprecated: Functions that expose MonoGenericContext
are going away in mono 4.0
Syntax
MonoClass*
mono_class_get_full (MonoImage *image, guint32 type_token, MonoGenericContext *context)
Parameters
image | the image where the class resides |
type_token | the token for the class |
context | the generic context used to evaluate generic instantiations in |
Return value
The MonoClass
that represents type_token in image
mono_class_get_image
Syntax
MonoImage*
mono_class_get_image (MonoClass *klass)
Return value
The image where this class is defined.
Description
Use this method to get the
MonoImage*
where this class came from.
mono_class_get_interfaces
Syntax
MonoClass*
mono_class_get_interfaces (MonoClass* klass, gpointer *iter)
Parameters
klass | the MonoClass to act on |
Return value
a MonoClass*
on each invocation, or NULL
when no more are available.
Description
This routine is an iterator routine for retrieving the interfaces implemented by this class.
You must pass a
gpointer
that points to zero and is treated as an opaque handle to
iterate over all of the elements. When no more values are
available, the return value is
NULL
.
mono_class_get_name
Syntax
const char*
mono_class_get_name (MonoClass *klass)
Parameters
klass | the MonoClass to act on |
Return value
The name of the class.
Description
mono_class_get_namespace
Syntax
const char*
mono_class_get_namespace (MonoClass *klass)
Parameters
klass | the MonoClass to act on |
Return value
The namespace of the class.
Description
mono_class_get_nesting_type
Syntax
MonoClass*
mono_class_get_nesting_type (MonoClass *klass)
Parameters
klass | the MonoClass to act on |
Return value
The container type where this type is nested or NULL
if this type is not a nested type.
Description
Use this function to obtain the class that the provided
MonoClass*
is nested on.
If the return is
NULL
, this indicates that this class is not nested.
mono_class_get_parent
Syntax
MonoClass*
mono_class_get_parent (MonoClass *klass)
Parameters
klass | the MonoClass to act on |
Return value
The parent class for this class.
Description
mono_class_get_property_token
Syntax
guint32
mono_class_get_property_token (MonoProperty *prop)
Parameters
prop | MonoProperty to query |
Return value
The ECMA token for the specified property.
Description
mono_class_get_rank
Syntax
int
mono_class_get_rank (MonoClass *klass)
Parameters
klass | the MonoClass to act on |
Return value
The rank for the array (the number of dimensions).
Description
mono_class_get_type
Syntax
MonoType*
mono_class_get_type (MonoClass *klass)
Parameters
klass | the MonoClass to act on |
Return value
The MonoType
from the class.
Description
This method returns the internal
MonoType
representation for the class.
mono_class_get_type_token
Syntax
guint32
mono_class_get_type_token (MonoClass *klass)
Parameters
klass | the MonoClass to act on |
Return value
The type token for the class.
Description
This method returns type token for the class.
mono_class_implements_interface
Syntax
mono_bool
mono_class_implements_interface (MonoClass* klass, MonoClass* iface)
Parameters
klass | The MonoClass to act on |
interface | The interface to check if klass implements. |
Return value
TRUE
if klass implements interface.
Description
mono_class_inflate_generic_method
Syntax
MonoMethod*
mono_class_inflate_generic_method (MonoMethod *method, MonoGenericContext *context)
Parameters
method | a generic method |
context | a generics context |
Return value
The new instantiated method
Description
Instantiate the generic method
method using the generics context
context.
mono_class_inflate_generic_type
Deprecated: Please use mono_class_inflate_generic_type_checked
instead
Syntax
MonoType*
mono_class_inflate_generic_type (MonoType *type, MonoGenericContext *context)
Parameters
type | a type |
context | a generics context |
Return value
The instantiated type or a copy of type. The returned MonoType
is allocated
on the heap and is owned by the caller. Returns NULL
on error.
Description
If
type is a generic type and
context is not
NULL
, instantiate it using the
generics context
context.
mono_class_init
Syntax
mono_bool
mono_class_init (MonoClass *klass)
Parameters
klass | the class to initialize |
Return value
TRUE
on success or FALSE
if there was a problem in loading
the type (incorrect assemblies, missing assemblies, methods, etc).
Description
Compute the
instance_size
,
class_size
and other infos that cannot be
computed at
mono_class_get
time. Also compute vtable_size if possible.
Initializes the following fields in
klass:
- all the fields initialized by
mono_class_init_sizes
- has_cctor
- ghcimpl
- inited
LOCKING: Acquires the loader lock.
mono_class_instance_size
Syntax
gint32
mono_class_instance_size (MonoClass *klass)
Parameters
Return value
The size of an object instance
Description
Use to get the size of a class in bytes.
mono_class_is_assignable_from
Syntax
mono_bool
mono_class_is_assignable_from (MonoClass *klass, MonoClass *oklass)
Parameters
klass | the class to be assigned to |
oklass | the source class |
Return value
TRUE
if an instance of class oklass can be assigned to an
instance of class klass
Description
mono_class_is_delegate
Syntax
mono_bool
mono_class_is_delegate (MonoClass *klass)
Parameters
klass | the MonoClass to act on |
Return value
TRUE
if the MonoClass
represents a System.Delegate
.
Description
mono_class_is_enum
Syntax
gboolean
mono_class_is_enum (MonoClass *klass)
Parameters
klass | the MonoClass to act on |
Return value
TRUE
if the MonoClass
represents an enumeration.
Description
Use this function to determine if the provided
MonoClass*
represents an enumeration.
mono_class_is_subclass_of
Syntax
gboolean
mono_class_is_subclass_of (MonoClass *klass, MonoClass *klassc, gboolean check_interfaces)
Parameters
klass | class to probe if it is a subclass of another one |
klassc | the class we suspect is the base class |
check_interfaces | whether we should perform interface checks |
Description
This method determines whether
klass is a subclass of
klassc.
If the
check_interfaces flag is set, then if
klassc is an interface
this method return
TRUE
if the
klass implements the interface or
if
klass is an interface, if one of its base classes is
klass.
If
check_interfaces is false, then if
klass is not an interface,
it returns
TRUE
if the
klass is a subclass of
klassc.
if
klass is an interface and
klassc is
System.Object
, then this function
returns
TRUE
.
mono_class_is_valuetype
Syntax
gboolean
mono_class_is_valuetype (MonoClass *klass)
Parameters
klass | the MonoClass to act on |
Return value
TRUE
if the MonoClass
represents a ValueType
, FALSE
if it represents a reference type.
Description
Use this method to determine if the provided
MonoClass*
represents a value type,
or a reference type.
mono_class_min_align
Syntax
gint32
mono_class_min_align (MonoClass *klass)
Parameters
Return value
minimum alignment requirements
Description
Use to get the computed minimum alignment requirements for the specified class.
mono_class_name_from_token
Syntax
char*
mono_class_name_from_token (MonoImage *image, guint32 type_token)
mono_class_num_events
Syntax
int
mono_class_num_events (MonoClass *klass)
Parameters
klass | the MonoClass to act on |
Return value
The number of events in the class.
Description
mono_class_num_fields
Syntax
int
mono_class_num_fields (MonoClass *klass)
Parameters
klass | the MonoClass to act on |
Return value
The number of static and instance fields in the class.
Description
mono_class_num_methods
Syntax
int
mono_class_num_methods (MonoClass *klass)
Parameters
klass | the MonoClass to act on |
Return value
The number of methods in the class.
Description
mono_class_num_properties
Syntax
int
mono_class_num_properties (MonoClass *klass)
Parameters
klass | the MonoClass to act on |
Return value
The number of properties in the class.
Description
mono_class_value_size
Syntax
gint32
mono_class_value_size (MonoClass *klass, guint32 *align)
Parameters
Return value
the size of a value of kind klass
Description
This function is used for value types, and return the
space and the alignment to store that kind of value object.
mono_class_vtable
Syntax
MonoVTable*
mono_class_vtable (MonoDomain *domain, MonoClass *klass)
Parameters
domain | the application domain |
class | the class to initialize |
Description
VTables are domain specific because we create domain specific code, and
they contain the domain specific static class data.
On failure, NULL
is returned, and class->exception_type
is set.
Getting Class Members
mono_class_get_fields
Syntax
MonoClassField*
mono_class_get_fields (MonoClass* klass, gpointer *iter)
Parameters
klass | the MonoClass to act on |
Return value
a MonoClassField*
on each iteration, or NULL
when no more fields are available.
Description
This routine is an iterator routine for retrieving the fields in a class.
You must pass a
gpointer
that points to zero and is treated as an opaque handle to
iterate over all of the elements. When no more values are
available, the return value is
NULL
.
mono_class_get_field_from_name
Syntax
MonoClassField*
mono_class_get_field_from_name (MonoClass *klass, const char *name)
Parameters
klass | the class to lookup the field. |
name | the field name |
Return value
The MonoClassField
pointer of the named field or NULL
Description
Search the class
klass and its parents for a field with the name
name.
mono_class_get_methods
Syntax
MonoMethod*
mono_class_get_methods (MonoClass* klass, gpointer *iter)
Parameters
klass | the MonoClass to act on |
Return value
a MonoMethod
on each iteration or NULL
when no more methods are available.
Description
This routine is an iterator routine for retrieving the fields in a class.
You must pass a
gpointer
that points to zero and is treated as an opaque handle to
iterate over all of the elements. When no more values are
available, the return value is
NULL
.
mono_class_get_method_from_name
Syntax
MonoMethod*
mono_class_get_method_from_name (MonoClass *klass, const char *name, int param_count)
Parameters
klass | where to look for the method |
name | name of the method |
param_count | number of parameters. -1 for any number. |
Description
Obtains a
MonoMethod
with a given name and number of parameters.
It only works if there are no multiple signatures for any given method name.
mono_class_get_method_from_name_flags
Syntax
MonoMethod*
mono_class_get_method_from_name_flags (MonoClass *klass, const char *name, int param_count, int flags)
Parameters
klass | where to look for the method |
name_space | name of the method |
param_count | number of parameters. -1 for any number. |
flags | flags which must be set in the method |
Description
Obtains a
MonoMethod
with a given name and number of parameters.
It only works if there are no multiple signatures for any given method name.
mono_class_get_nested_types
Syntax
MonoClass*
mono_class_get_nested_types (MonoClass* klass, gpointer *iter)
Parameters
klass | the MonoClass to act on |
Return value
a Monoclass*
on each invocation, or NULL
when no more are available.
Description
This routine is an iterator routine for retrieving the nested types of a class.
This works only if
klass is non-generic, or a generic type definition.
You must pass a
gpointer
that points to zero and is treated as an opaque handle to
iterate over all of the elements. When no more values are
available, the return value is
NULL
.
mono_class_get_properties
Syntax
MonoProperty*
mono_class_get_properties (MonoClass* klass, gpointer *iter)
Parameters
klass | the MonoClass to act on |
Return value
a MonoProperty*
on each invocation, or NULL
when no more are available.
Description
This routine is an iterator routine for retrieving the properties in a class.
You must pass a gpointer that points to zero and is treated as an opaque handle to
iterate over all of the elements. When no more values are
available, the return value is
NULL
.
mono_class_get_property_from_name
Syntax
MonoProperty*
mono_class_get_property_from_name (MonoClass *klass, const char *name)
Parameters
klass | a class |
name | name of the property to lookup in the specified class |
Return value
the MonoProperty
with the given name, or NULL
if the property
does not exist on the klass.
Description
Use this method to lookup a property in a class
mono_class_get_events
Syntax
MonoEvent*
mono_class_get_events (MonoClass* klass, gpointer *iter)
Parameters
klass | the MonoClass to act on |
Return value
a MonoEvent*
on each invocation, or NULL
when no more are available.
Description
This routine is an iterator routine for retrieving the properties in a class.
You must pass a
gpointer
that points to zero and is treated as an opaque handle to
iterate over all of the elements. When no more values are
available, the return value is
NULL
.
Fields
mono_field_get_data
Syntax
const char *
mono_field_get_data (MonoClassField *field)
Parameters
field | the MonoClassField to act on |
Return value
A pointer to the metadata constant value or to the field
data if it has an RVA flag.
Description
mono_field_get_offset
Syntax
guint32
mono_field_get_offset (MonoClassField *field)
Parameters
field | the MonoClassField to act on |
Return value
The field offset.
Description
mono_field_full_name
Syntax
char*
mono_field_full_name (MonoClassField *field)
Parameters
field | field to retrieve information for |
Return value
the full name for the field, made up of the namespace, type name and the field name.
Method Accessibility
mono_method_can_access_field
Syntax
gboolean
mono_method_can_access_field (MonoMethod *method, MonoClassField *field)
Parameters
method | Method that will attempt to access the field |
field | the field to access |
Return value
TRUE
if the given method is allowed to access the field while following
the accessibility rules of the CLI.
Description
Used to determine if a method is allowed to access the specified field.
mono_method_can_access_method
Syntax
gboolean
mono_method_can_access_method (MonoMethod *method, MonoMethod *called)
Parameters
method | Method that will attempt to access the other method |
called | the method that we want to probe for accessibility. |
Return value
TRUE
if the given method is allowed to invoke the called while following
the accessibility rules of the CLI.
Description
Used to determine if the
method is allowed to access the specified
called method.