jbevain / cecil

Cecil is a library to inspect, modify and create .NET programs and libraries.
MIT License
2.76k stars 627 forks source link

`IsValueType` not set correctly for custom attribute fields of an enum type #941

Open Zastai opened 6 months ago

Zastai commented 6 months ago

All parts of the TypeReference for such an enum are right, it's just not flagged as a value type. As a result, using that TypeReference elsewhere results in load/resolve-time errors when using the modified assembly.

Looks like the reading for custom attribute fields/properties (AssemblyReader.ReadCustomAttributeFieldOrPropType) sees ElementType.Enum and then bases the TypeReference on the type name it finds.

ltrzesniewski commented 6 months ago

Hmm, this reminds me of something: https://github.com/ltrzesniewski/InlineIL.Fody/blob/b257467995801ee2f3a4db44aef86acfcceccbc9/src/InlineIL.Fody/Model/TypeRefBuilder.cs#L174-L181

I wrote the following comment:

TypeRefs from different assemblies get imported as MetadataType.Class since this information is not stored in the assembly metadata.

IIRC this applies to all value types, not only enums, since the TypeRef metadata table does not store this flag.

Zastai commented 6 months ago

Yes, for the general case where there is just an utf8 type name to be resolved in the typeref table, that's understandable.

But here the ElementType adds that extra bit of information, because (afaik) all enums are value types.