MakeGenericType and MakeArrayType on TypeDefinition ?

452 views
Skip to first unread message

impworks

unread,
Dec 6, 2010, 8:03:14 AM12/6/10
to mono-cecil
Hello!

Is there any way to programmatically turn a type described by
TypeDefinition to an array of that type?

I'm trying to create a compiler for a language, in which the user can
declare variables as:

<code>
int a;
int[] b;
int[][] c;
</code>

and so on. The trick is, I don't know the arity of the the array the
user desires to create, and the type may be built-in or user-defined
too.
When working with System.Type, I can create an array using this block
of code:

<code>
var someType = typeof(MyType);
var arrayType = someType.MakeArrayType();
</code>

The same way, I need to be able to wrap a type into a List<T>.

Is there any analog that works like this on a TypeDefinition instead
of Type? Or maybe I can get a Type out of TypeDefinition, process it
and create a new TypeDefinition by using Assembly.MainModule.Import() ?

Jb Evain

unread,
Dec 6, 2010, 8:06:38 AM12/6/10
to mono-...@googlegroups.com
Hey,

On Mon, Dec 6, 2010 at 2:03 PM, impworks <hazar...@gmail.com> wrote:
> Is there any way to programmatically turn a type described by
> TypeDefinition to an array of that type?

TypeDefinition type = GetTypeDefinition ();
TypeReference array = new ArrayType (type);

Alternatively, you can use the MakeArrayType extension method in Cecil.Rocks.

Jb

Timwi

unread,
Dec 6, 2010, 9:27:06 AM12/6/10
to mono-cecil
Jb has already answered the array question for you. In order to wrap
something in List<T>, simply use “new GenericInstanceType(...)”
instead.

However, note that the result is *not* a TypeDefinition.
TypeDefinition only refers to real types that are actually declared in
your assembly. ArrayType and GenericInstanceType will be subclasses of
*TypeReference*, which is used to *refer* to types rather than
*define* them. Of course, TypeDefinition is itself a subclass of
TypeReference so that it can be used to refer to the type itself.

All of this could probably be simply explained in an XML comment on
the classes in Mono.Cecil, but Jb refuses to add any of those without
explaining to us why.

Timwi

Greg Young

unread,
Dec 6, 2010, 9:33:28 AM12/6/10
to mono-...@googlegroups.com
I think this could be a great thing that we could all help with as well.

OSS != one person works and lots of people consume.

JB could you go more into detail on how you see things working? I for one would be happy to help out.

Cheers,

Greg

On Mon, Dec 6, 2010 at 9:31 AM, Jb Evain <jbe...@gmail.com> wrote:
Dude,


On Mon, Dec 6, 2010 at 3:27 PM, Timwi <ti...@gmx.net> wrote:
> All of this could probably be simply explained in an XML comment on
> the classes in Mono.Cecil, but Jb refuses to add any of those without
> explaining to us why.

This is not really accurate.

The piece of text you just wrote would perfectly fit on a doc, be it
in the wiki or in a xml doc. I already said in the past that I don't
like inline XML comments.

I'm definitely not against having an API documentation using monodoc
which stores the documentation outside of the source.

Jb

--
--
mono-cecil



--
Les erreurs de grammaire et de syntaxe ont été incluses pour m'assurer de votre attention

Jb Evain

unread,
Dec 6, 2010, 9:36:09 AM12/6/10
to mono-...@googlegroups.com
On Mon, Dec 6, 2010 at 3:33 PM, Greg Young <gregor...@gmail.com> wrote:
> I think this could be a great thing that we could all help with as well.
> OSS != one person works and lots of people consume.

To be fair, Timwi already said he was willing to help with the doc.

> JB could you go more into detail on how you see things working? I for one
> would be happy to help out.

I'll see about generating monodoc stubs and check them in. Everyone
could contribute to it then.

Jb

Greg Young

unread,
Dec 6, 2010, 9:38:39 AM12/6/10
to mono-...@googlegroups.com
Not trying to single anyone out or imply anything, just trying to move it to a productive discussion.

--
--
mono-cecil

impworks

unread,
Dec 6, 2010, 11:34:04 AM12/6/10
to mono-cecil
Thanks for all the replies! It was really helpful and I might be a
step closer to implementing my project.

Another question arose though, how do I work with a
GenericInstanceType?
If I want to create a TypeReference to List<Someclass>, do I create a
GenericInstanceType from List or from Someclass?

Jb Evain

unread,
Dec 6, 2010, 11:36:58 AM12/6/10
to mono-...@googlegroups.com
On Mon, Dec 6, 2010 at 5:34 PM, impworks <hazar...@gmail.com> wrote:
> Another question arose though, how do I work with a
> GenericInstanceType?
> If I want to create a TypeReference to List<Someclass>, do I create a
> GenericInstanceType from List or from Someclass?

The following code implies that both list_t and Someclass are defined
in another module, and thus, require to be imported.

ModuleDefinition module = ...;
var list_t = module.Import (list_t_definition);
var some_class = module.Import (some_class_definition);

var list_some_class = new GenericInstanceType (list_t);
list_come_class.GenericArguments.Add (some_class);

Jb

impworks

unread,
Dec 6, 2010, 11:39:42 AM12/6/10
to mono-cecil
Thanks, I got it!

Do I import every single type I use, including the super-default one
like void, int, etc?

Jb Evain

unread,
Dec 6, 2010, 11:45:23 AM12/6/10
to mono-...@googlegroups.com
On Mon, Dec 6, 2010 at 5:39 PM, impworks <hazar...@gmail.com> wrote:
> Do I import every single type I use, including the super-default one
> like void, int, etc?

For those you have fast accessors:

module.TypeSystem.Void

For instance.

Jb

Reply all
Reply to author
Forward
0 new messages