¿Cómo determinar si un tipo es un tipo de valor de los metadatos de la CLI?

Estoy usando el IMetaDataImport API and and I need to determine if the type definitions in my assembly are value or reference types. I have not found any method in the API that explicitely provides this information. However, I can easily enough obtain the base type and check if it is System.ValueType.

My question is: is the base class the only way through which CLI metadata encodes the fact that a type is a value type or are there some flags for this somewhere?

preguntado el 01 de julio de 12 a las 00:07

2 Respuestas

There are no flags that identify value types directly. It looks like checking the base type, along with a couple of other things, is the canonical way to do it.

Section II.10.1.3 of the Especificaciones de ECMA CLI describes the type definition like this:

The type semantic attributes specify whether an interface, class, or value type shall be defined. The interfaz. attribute specifies an interface. If this attribute is not present and the definition extends (directly or indirectly) System.ValueType, and the definition is not for System.Enum, a value type shall be defined. Otherwise, a class shall be defined.

Respondido 01 Jul 12, 01:07

Cool, that's clear enough! The "indirectly" part is a bit surprising, though. Does it refer only to enumerations, which inherit from System.Enum? I don't see any other legal form of indirect System.ValueType herencia. - Trillian

@Trillian: Yes, I think that's right. System.ValueType es un tipo de clase, System.Enum itself is a class type inheriting from System.ValueType, but anything else in that inheritance hierarchy will be a value-type. - LucasH

Es el Type.IsValueType perfecta ¿Que estas buscando?

Respondido 01 Jul 12, 01:07

Not quite, the fact that this property exists does not indicate that the metadata directly exposes this value. However, you gave me the idea to look at Mono's implementation of this property and it indeeds confirms that value type-ness is determined by checking the inheritance chain. - Trillian

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.