Bugzilla – Bug 319019
[GMCS] Getting a generic struct via reflection fails
Last modified: 2007-09-15 21:24:46 UTC
---- Reported by kiess@h3c.de 2005-09-17 09:13:57 MST ---- Description of Problem: When reading a generic struct with System.Reflection.FieldInfo.GetValue the returned value is wrong. Steps to reproduce the problem: 1. cat > test.cs << EOF public struct Container<T> { public T content; public Container (T content) { this.content = content; } } public class A { public Container<int> field; public A () { field = new Container<int> (1); } } public class M { public static void Main() { A a = new A(); System.Console.WriteLine (a.field.content); object o = a.GetType().GetField("field").GetValue (a); Container<int> unboxed = (Container<int>)o; System.Console.WriteLine (unboxed.content); } } EOF 2. gmcs test.cs 3. mono test.exe Actual Results: 1 0 Expected Results: 1 1 How often does this happen? always Additional Information: ---- Additional Comments From vargaz@gmail.com 2005-11-15 09:23:44 MST ---- For the record: This is caused by the fact that mono_type_get_underlying_type () returns a MonoType for GENERICINST types whose data.klass field points to the 'generic' generic class, not the 'specific' generic class created by mono_class_create_generic (). So either mono_type_get_underlying_type () needs to return the 'specific' generic class (and the byval_arg field of this class needs to point to itself and not to the generic class), or the code calling get_underlying_type () needs to call mono_class_from_mono_type () instead of accessing type->data.klass directly. There are lots of places inside the runtime where this bug manifests itself. For example, https://bugzilla.novell.com/show_bug.cgi?id=MONO75479 is caused by the same problem. ---- Additional Comments From martin@ximian.com 2005-11-15 13:53:48 MST ---- Fixed in SVN. ---- Additional Comments From martin@ximian.com 2005-11-15 14:01:45 MST ---- . This bug blocked bug(s) 75735. Unknown operating system unknown. Setting to default OS "Other".