Convert Int32 to int type name using reflection - how?

  • Thread starter Thread starter Sergey M
  • Start date Start date
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.
 
Sergey,

If you use CodeDom for code generation it will handle this for you.

If you don't, you'll have to use your own translation table for C#.
There aren't that many type keywords in C#.

For VB.NET, you can use the function VbTypeName in the VB library.



Mattias
 
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.

Out of interest, why do you need to use int rather than Int32? They'll
compile to exactly the same code. There's no "actual" name - int is
just a shorthand for System.Int32, that's all.
 
Mattias,
If you use CodeDom for code generation it will handle this for you.
If you don't, you'll have to use your own translation table for C#.
There aren't that many type keywords in C#.

Unfortunately, I use type discovery on .NET assemblies for the method
return and parameters types and CodeDom is not the right approach to use
in my case.

Right, I've done it using my own translator for now but I was wondering
if there was a more elegant way of doing that.
For VB.NET, you can use the function VbTypeName in the VB library.

That's a great tip. Thank. I use C# primarily so I'm not very much
VB.NET versed, yet, I have to support it as well.

Thanks again.
 
Michael,
As far as Jon's question they only justification for using int instead
of Int32 is just for readability. Some inexperienced programmers may
not know int <==> System.Int32. Otherwise there really is no point.

Yes, my thought exactly - readability.
I would agree with using the Codedom why write something you don't
have to unless purely for educational value.

I wish I could but... As I replied to Mattias, I need to be able to
discover the method return and parameters types on existing assemblies
at run-time. I don't know of any other way of doing that but via
reflection. I get System. type via reflection hence my original
question.

Could CodeDom be helpfuly somehow in this case?

Thanks.
 
Back
Top