简体   繁体   中英

(Mono) How to get the MonoClass* of the class within another class?

I have a class within another class in the C# script, such as

namespace N{
  public class A{
    private class B{}
  }
}

Then I tried to get the assembly classes in Mono. When class B is retrieved, the following results will occur.

const MonoTableInfo* tableInfo = mono_image_get_table_info( image, MONO_TABLE_TYPEDEF );
uint32_t rows = mono_table_info_get_rows( tableInfo );

for ( uint32_t i = 0; i < rows; i++ )
{
  uint32_t cols[ MONO_TYPEDEF_SIZE ];
  mono_metadata_decode_row( tableInfo, i, cols, MONO_TYPEDEF_SIZE );
  const char* name = mono_metadata_string_heap( image, cols[ MONO_TYPEDEF_NAME ] ); // "B"
  const char* nameSpace = mono_metadata_string_heap( image, cols[ MONO_TYPEDEF_NAMESPACE ] ); // ""
  MonoClass* klass = mono_class_from_name( image, nameSpace, name ); // nullptr
}

After that, I tried to use mono_class_from_name definitely, but it does not work.

  MonoClass* klass1 = mono_class_from_name( image, "",  "A.B" ); // nullptr
  MonoClass* klass2 = mono_class_from_name( image, "N", "B"   ); // nullptr
  MonoClass* klass3 = mono_class_from_name( image, "N", "A.B" ); // nullptr

So how could I get the MonoClass* of the class within another class?

We need to format the name like this: MyNamespace.MyParentClass/MyNestedClass.

MonoClass* klass = mono_class_from_name( image, "N",  "A/B" ); // it works

The namespaces and parent classes of nested classes are not available through mono_metadata_string_heap . And We need a better and more reasonable way to separate the non-nested classes and nested classes here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM