Obtaining c# type declaration

L

Lawrence Kevin

Hi all

I am using reflection to obtain some FieldInfo objects, what I need to do
is convert the .Net type to the c# syntax - I can do this myself using my
own conversion but I was wondering if there was a quick and easy way - example:

Int32 would become int
String would be come string

Any suggestions? Should I just write my own?

Thanks
Kev
 
J

Jon Skeet [C# MVP]

Lawrence said:
I am using reflection to obtain some FieldInfo objects, what I need to do
is convert the .Net type to the c# syntax - I can do this myself using my
own conversion but I was wondering if there was a quick and easy way - example:

Int32 would become int
String would be come string

Any suggestions? Should I just write my own?

Yes, I don't think there's anything in the framework to do this for
you. Just have a map from Type to name, and use map[typeof(int)]="int";
etc. Then check for the presence of the type within the map, and use
Type.Name if it's not there. There aren't many to do, fortunately.

Jon
 
L

Lawrence Kevin

Lawrence said:
I am using reflection to obtain some FieldInfo objects, what I need
to do is convert the .Net type to the c# syntax - I can do this
myself using my own conversion but I was wondering if there was a
quick and easy way - example:

Int32 would become int
String would be come string
Any suggestions? Should I just write my own?
Yes, I don't think there's anything in the framework to do this for
you. Just have a map from Type to name, and use
map[typeof(int)]="int"; etc. Then check for the presence of the type
within the map, and use Type.Name if it's not there. There aren't many
to do, fortunately.

Jon

Thanks

Kev
 
R

Ranjan

Hello Lawrence,

No in built functionality to get type specific "primitive type name". Anyway,
there should not be any problem/issues with the ones you get using Reflection.
Otherwise, you might have to write a switch to convert BCL names to language
specific names.

r.
 
N

Nicholas Paldino [.NET/C# MVP]

Kev,

I don't understand what the difference is really, since C# will accept
String for "string" and Int32 for "int". They are aliases, and they don't
exclude the type names as they are defined in the framework.
 
J

Jon Skeet [C# MVP]

Nicholas Paldino said:
I don't understand what the difference is really, since C# will accept
String for "string" and Int32 for "int". They are aliases, and they don't
exclude the type names as they are defined in the framework.

Sure - but I'm guessing that this could be used for a code-generator or
something similar (documentation system?), where it would produce more
idiomatic C# code if it used "int" instead of "Int32" etc.
 
N

Nicholas Paldino [.NET/C# MVP]

Well, a statement could be made about one who doesn't know the
difference between Int32 and int... =)
 
J

Jon Skeet [C# MVP]

Nicholas Paldino said:
Well, a statement could be made about one who doesn't know the
difference between Int32 and int... =)

Why should it be a matter of knowing differences? Why can't it be a
simple matter of preference and familiarity? The MSDN library gives
declarations using the C# aliases (in the details of the method), and I
believe it's a better product because of that.
 
N

Nicholas Paldino [.NET/C# MVP]

In the general sense, I couldn't care less, it's all the same to me.

However, when dealing with the code generators, it's a little different,
since you would have to have an option (maybe one exists already, I don't
know, I haven't looked that much into it) to indicate that you should output
aliases.
 
K

Kevin Lawrence

I'm sticking to the C# keywords purely to follow coding-standards within
our department.

Kev
 

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