Convert Int32 to int type name using reflection - how?

S

Sergey M

Hi all,

I'm working on some code that creates C#/VB.NET source files. I use
reflection to examine some base class methods, their return types and
parameter types. When I use reflection I of course get .NET types ('Int32'
for instance).

The problem is that when building the source code files, I need to use the
actual ('int' for integral types or 'bool' for Boolean, for instance) type
names/keywords as oppose to .NET ones. And I also need to be able to
generate the code correctly for both C# and VB.NET.

I'm kind of stuck and would appreciate any ideas or suggestions. Thanks in
advance.
 
B

Bruno Jouhier [MVP]

System.Int32 and int are ALIASES in C# so the following declarations:

System.Int32 i;
int i;

are STRICLY equivalent.

So, the reflection API does not make any difference between System.Int32 and
int.

Bruno.
 
M

Miha Markic [MVP C#]

Hi Sergey,

Sergey M said:
Hi all,

I'm working on some code that creates C#/VB.NET source files. I use
reflection to examine some base class methods, their return types and
parameter types. When I use reflection I of course get .NET types ('Int32'
for instance).

The problem is that when building the source code files, I need to use the
actual ('int' for integral types or 'bool' for Boolean, for instance) type
names/keywords as oppose to .NET ones. And I also need to be able to
generate the code correctly for both C# and VB.NET.

Why do you need to use int and bool instead of .net ones?
As Bruno mentioned they are equivalent.
 
A

Andreas Håkansson

Sergey,

To clairify on what Bruno and Miha have told you. Keywords
such as int and bool are just aliases for the datatypes used in the
..NET Framework. When you compile your code it gets translated
back into the .NET names (which is infact the names of the
underlaying structs) so the code can be consumed from different
langauges. This is why for instance VB.NET can use an assembly
compiled in C# .. because they all build ontop of the Common
Type System (CTS).

Hope this helps,

//Andreas
 
S

Sergey M

Andreas,
Keywords
such as int and bool are just aliases for the datatypes used in the
.NET Framework.

I'm well aware of all of the keyword/alias and .NET type. The original
question still stand. I appreciate your reply though.
 
S

Sergey M

Bruno,
are STRICLY equivalent.
So, the reflection API does not make any difference between
System.Int32 and

Right, I am aware of that. I still need to be able to convert it.
Apparently VB.NET has VbTypeName function for that but I couldn't find
anything similar for C#. So, my question still stands. Thanks.
 
M

mikeb

Sergey said:
Miha,




Mainly for the readability of the generated code. Thanks.

I think you'll need to come up with your own function to convert .NET
type names into C# keywords. Not hard - there's only 15 of them. I'll
bet you've spent more time explaining the problem in the this thread!

Look in the C# reference for the "Built-in Type Table".
 
S

Sergey M

Mike,
I think you'll need to come up with your own function to convert .NET
type names into C# keywords. Not hard - there's only 15 of them.

Right, I've already had something like that in place. I was just looking
for more elegant/proper way of doing that. One thing I've ran into is
that in addition to that type list I had to add System.Void -> void
translation as well.
I'll bet you've spent more time explaining the problem in the this
thread!

Yeah, as you might've noticed <g>.
 
M

Miha Markic [MVP C#]

Hi Sergey,

Sergey M said:
Mike,


Right, I've already had something like that in place. I was just looking
for more elegant/proper way of doing that. One thing I've ran into is
that in addition to that type list I had to add System.Void -> void
translation as well.

No, there is no elegant solution.
Create a hashtable that maps the values.
thread!

Yeah, as you might've noticed <g>.

But community is learning in this way <g>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top