[Mono-dev] Mono.Cecil: Full names of generic types
mono at evain.net
Mon Jul 24 06:33:31 EDT 2006
Matej Urbas wrote:
> I have one question though:
> Suppose we have such a TypeReference: SomeType`2<int, T>
> Here is what I do to get all info about "SomeType`2" out of there:
> I have a loop that goes like this:
> Check if the TypeReference is of any of these types:
> 1. ArrayType
> 2. GenericInstanceType
> 3. ReferenceType
> 4. PointerType
> 5. None of the above
> if we stumble on either of cases 1-4, we can cast the TypeReference to
> either of them and gather whatever additional info they have (e.g.:
> ArrayType gives us info about the Rank, GenericInstanceType gives us
> info about GenericAttributes, ReferenceType tells us that the type is
> passed ByRef and PointerType gives us the level of indirection).
> Moreover, each of the above types has a property named 'ElementType'
> which is the one I use to gather all info about the type.
> Now in the 5th case, we have only the plain type - without the array
> modifier or any generic attributes - which is our end point...
> Here is what my algorithm would do (looking at SomeType`2<int, T>):
> Step 1: current type is an ArrayType - get its rank <-- now visit its
> Step 2: current type is an ArrayType - get its rank <-- now visit its
> Step 3: current type is a GenericInstanceType - traverse its
> GenericArguments <--- now visit its ElementType
> Step 4: current type falls into the 5th case (its neither an ArrayType
> or GenericInstanceType or whatever) - it is simply a TypeReference: with
> the name "SomeType`2" <--- without any decorations... <--- I store this
> name in MonoDevelop
There is a simpler way. The inheritance tree is like this:
You can say that if the type is a TypeSpecification, you loop until its
ElementType property is not a TypeSpecification itself. You can then
check which kind of TypeSpec it is.
Please note that a visitor approach would probably be much better, and
that I'll have to modify Cecil one day to handle this.
> My question is if there are any other possible TypeReference types
> (other then the 4 mentioned above)? Did I miss something?
> I see. Do you have a case where we would stumble upon a MethodReference?
> I mean, if we go through all types in the MainModule of an assembly,
> would we find a method reference with actual arguments specified? Do you
> have a C# example of such an occurrence?
You can find MethodReferences only in the MemberReference collection of
the Module. You'll encounter them as operands of the opcodes in the
bodies of the methods. They are only markers.
> BTW, Cecil rocks! Great work!
More information about the Mono-devel-list