提交 b07a3824 编写于 作者: Z Zoltan Varga

Fix the setting of pointer type fields using reflection. Fixes #641590.

上级 502b8e26
......@@ -74,7 +74,7 @@ namespace MonoTests.System.Reflection
}
[TestFixture]
public class FieldInfoTest
public unsafe class FieldInfoTest
{
[NonSerialized]
public int i;
......@@ -479,6 +479,20 @@ namespace MonoTests.System.Reflection
Assert.AreEqual (IntEnum.Third, typeof(Foo<>).GetField ("econstant").GetValue (null), "#3");
}
public static unsafe void* ip;
[Test]
public unsafe void GetSetValuePointers ()
{
int i = 5;
void *p = &i;
typeof (FieldInfoTest).GetField ("ip").SetValue (null, (IntPtr)p);
Pointer p2 = (Pointer)typeof (FieldInfoTest).GetField ("ip").GetValue (null);
int *pi = (int*)Pointer.Unbox (p2);
Assert.AreEqual (5, *pi);
}
public class Foo<T>
{
/*
......
......@@ -1823,6 +1823,7 @@ ves_icall_MonoField_SetValueInternal (MonoReflectionField *field, MonoObject *ob
case MONO_TYPE_I8:
case MONO_TYPE_R8:
case MONO_TYPE_VALUETYPE:
case MONO_TYPE_PTR:
if (v != NULL)
v += sizeof (MonoObject);
break;
......
......@@ -2994,6 +2994,7 @@ mono_field_get_value_object (MonoDomain *domain, MonoClassField *field, MonoObje
gboolean is_static = FALSE;
gboolean is_ref = FALSE;
gboolean is_literal = FALSE;
gboolean is_ptr = FALSE;
MonoError error;
MonoType *type = mono_field_get_type_checked (field, &error);
......@@ -3028,6 +3029,9 @@ mono_field_get_value_object (MonoDomain *domain, MonoClassField *field, MonoObje
case MONO_TYPE_GENERICINST:
is_ref = !mono_type_generic_inst_is_valuetype (type);
break;
case MONO_TYPE_PTR:
is_ptr = TRUE;
break;
default:
g_error ("type 0x%x not handled in "
"mono_field_get_value_object", type->type);
......@@ -3067,6 +3071,34 @@ mono_field_get_value_object (MonoDomain *domain, MonoClassField *field, MonoObje
return o;
}
if (is_ptr) {
static MonoMethod *m;
gpointer args [2];
gpointer *ptr;
gpointer v;
if (!m) {
MonoClass *ptr_klass = mono_class_from_name_cached (mono_defaults.corlib, "System.Reflection", "Pointer");
m = mono_class_get_method_from_name_flags (ptr_klass, "Box", 2, METHOD_ATTRIBUTE_STATIC);
g_assert (m);
}
v = &ptr;
if (is_literal) {
get_default_field_value (domain, field, v);
} else if (is_static) {
mono_field_static_get_value (vtable, field, v);
} else {
mono_field_get_value (obj, field, v);
}
/* MONO_TYPE_PTR is passed by value to runtime_invoke () */
args [0] = *ptr;
args [1] = mono_type_get_object (mono_domain_get (), type);
return mono_runtime_invoke (m, NULL, args, NULL);
}
/* boxed value type */
klass = mono_class_from_mono_type (type);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册