On 1 Oct 2013, at 13:03, Robert Jordan <[email protected]> wrote:

> On 01.10.2013 11:37, [email protected] wrote:
>> Is it the case that calling a static field via the embedded API does not 
>> cause the static constructor to be run?
>> This seems to be the behaviour I am observing.
>> 
>> In the example below setting the static MetaDataXXX fields (before any other 
>> static access) does not cause the static constructor to be run and expected 
>> behaviour is lost.
>> If I call a static warmup method first, which does trigger the constructor, 
>> then all is well.
> 
> You're supposed to call mono_runtime_class_init () on the vtable
> of the class before calling mono_field_static_get/set.
> 
> Robert

There doesn't seem to be anything in the docs about this.
Is it just a kludge?

in metadata/object.c/mono_field_get_value_object we see

if (type->attrs & FIELD_ATTRIBUTE_STATIC) {
                is_static = TRUE;

                if (!is_literal) {
                        vtable = mono_class_vtable_full (domain, field->parent, 
TRUE);
                        if (!vtable->initialized)
                                mono_runtime_class_init (vtable);
                }
        } else {
                g_assert (obj);
        }

However, kludge or not it does seem to work:

void DBMonoClassGetField(MonoClass *monoClass, const char *fieldName, void 
*valueObject) {
        MonoClassField *field = mono_class_get_field_from_name(monoClass, 
fieldName);
        MonoVTable *vtable = mono_class_vtable(mono_domain_get(), monoClass);
        mono_runtime_class_init(vtable);
    
        mono_field_static_get_value(vtable, field, valueObject);
}

void DBMonoClassSetField(MonoClass *monoClass, const char *fieldName, 
MonoObject *valueObject) {
        MonoClassField *field = mono_class_get_field_from_name(monoClass, 
fieldName);
        MonoVTable *vtable = mono_class_vtable(mono_domain_get(), monoClass);
        mono_runtime_class_init(vtable);
    
        mono_field_static_set_value(vtable, field, valueObject);
}

Jonathan
_______________________________________________
Mono-list maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-list

Reply via email to