+ MONO_HANDLE_SETVAL (ref_tb, state, gint32, MonoTypeBuilderFinished);
+
+ reflection_init_generic_class (ref_tb, error);
+ if (!is_ok (error))
+ goto leave;
+
+ // Do here so that the search inside of the parent can see the above type that's been set.
+ MonoReflectionTypeHandle ref_parent = MONO_HANDLE_NEW_GET (MonoReflectionType, ref_tb, parent);
+ MonoType *parent_type = NULL;
+ if (!MONO_HANDLE_IS_NULL (ref_parent)) {
+ MonoClass *parent_klass = mono_handle_class (ref_parent);
+ gboolean recursive_init = TRUE;
+
+ if (is_sre_type_builder (parent_klass)) {
+ MonoTypeBuilderState parent_state = MONO_HANDLE_GETVAL (MONO_HANDLE_CAST (MonoReflectionTypeBuilder, ref_parent), state);
+
+ if (parent_state != MonoTypeBuilderNew) {
+ // Initialize types reachable from parent recursively
+ // We'll fix the type hierarchy later
+ recursive_init = FALSE;
+ }
+ }
+
+ if (recursive_init) {
+ // If we haven't encountered a cycle, force the creation of ref_parent's type
+ mono_reflection_type_handle_mono_type (ref_parent, error);
+ if (!is_ok (error))
+ goto leave;
+ }
+
+ parent_type = MONO_HANDLE_GETVAL (ref_parent, type);
+
+ // If we failed to create the parent, fail the child
+ if (!parent_type)
+ goto leave;
+ }
+
+ // Push the child type and parent type to process later
+ // Note: parent_type may be null.
+ g_assert (!g_hash_table_lookup (unparented_classes, &klass->byval_arg));
+ g_hash_table_insert (unparented_classes, &klass->byval_arg, parent_type);