This project is read-only.

Custom Attributes with System.Type arguments

Topics: Metadata Model
Sep 17, 2012 at 7:33 PM


I'm having trouble retrieving the System.Type instance corresponding to Custom Attribute named argument.

In particular, the IMetadataConstant object retrieved using IMetadataNamedArgument.ArgumentValue has a Type property that is an INamedTypeReference corresponding to System.Type in mscorlib, but the Value property is an Int32 object.

The metadata specification indicates that System.Type values are encoded as strings, so I was expecting either a resolved System.Type object or a System.String object, and I have no idea what to do with the Int32 value.

Any help appreciated!


Sep 17, 2012 at 8:23 PM

Update: in trying to come up with a short repro I see that CCI is correctly creating a TypeOfExpression object, so there's something peculiar about the assembly I am using, and I will attempt to narrow down the cause.

Sep 17, 2012 at 8:48 PM

Update: it appears that this is related to facade assemblies and type forwarders. In particular, the Type class in the System.Runtime facade is not being unified with the Type class in mscorlib. I made a small change to Microsoft.Cci.MetadataReader.AttributeDecoder.ReadSerializedValue to pass 'true' to TypeHelper.TypesAreEquivalent when testing if the incoming constant type is equivalent to this.PEFileToObjectModel.PlatformType.SystemType, and this appears to have addressed the issue, but I'm not sure if this is the right thing to do or if this would negatively interfere with something elsewhere. Alternatively, it appears that I could just pass in 'typeDef' instead of 'type' to achieve the same thing.

Sep 17, 2012 at 9:59 PM

Ok, figured it out - sorry for the noise. I needed to use MetataReaderHost.AddLibPath(<path to contract assemblies>), and after that everything works great!