[Mono-list] Are constants accessible via reflection?

Holger Arnold harnold@gmx.de
Tue, 6 Aug 2002 18:22:45 +0200


Hello,

lupus, thank you for your quick answer. Some further thoughts of mine:
> > for a research project I'm developing a compiler that targets the CLI.
> > This compiler, which is developed partly in C# using Mono, has to store
> > additional semantic information in the compiled PE files. The information
> > will be stored either in the Constant table (0x0B), or in the data
> > section of the PE file, referenced through the FieldRVA table (0x1D).
>
> You could also use custom attributes.

Yes, I though about that. The information that is attached to the types is
encoded rather low-level in byte arrays. The CLI spec, part. II, describes in
22.3 how arrays can encoded as arguments for custom attributes. But, I don't
know how to do this from within C#, so I was not shure if attributes with
array parameters had any disadvantages. Does anyone know...?

> The constant table can hold only simple types, while you can set an
> arbitrary blob of data in the FieldRVA table.

Ok, I see. But why don't they store simple types directly in the Value column
of the constant table, instead of an index into the Blob heap? Does this 
mean, that e.g. string constants are stored as index into the Blob heap, 
where an index into the string heap is stored? Or is the spec not exact here?

> > accessible via the standard System.Reflection mechanism, or if I have
> > to use a more low-level metadata library?
>
> field.GetValue() should work.

I don't think that GetValue() will work, because the runtime does not
automatically initialize fields with their field init meta-data, they are
only zero'ed out.

> See also how the C# compiler handles array initialization like:
> 	int[] array = {0, 1, 2, 3, 4, 5, 6};

The Microsoft compiler uses a special InitializeArray() method. This is not
possible for me, because some of the information must also be accessible
outside the CLI, so no types can be instantiated and no methods called.

> > There should be a way to access such data via System.Reflection, as at
> > least constants can easily be generated via the SetConstant() method of
> > the System.Reflection.Emit.FieldBuilder class, but I'm unable to find out
> > how.
>
> There is also SetRVAData().

Yes, but all I needed was a FieldInfo.GetConstant() method. Anyone hearing me
in Redmont...?

Regards,
Holger

-------------------------------------------------------