471,317 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,317 software developers and data experts.

Obtaining c# type declaration


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
Jan 20 '06 #1
9 1367
Lawrence Kevin wrote:
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

Jan 20 '06 #2
> Lawrence Kevin wrote:
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
Jan 20 '06 #3
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.

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

Jan 20 '06 #4
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Lawrence Kevin" <sp******@spam.com> wrote in message
news:3a*************************@news.rmplc.co.uk. ..

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

Jan 21 '06 #5
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 21 '06 #6
Well, a statement could be made about one who doesn't know the
difference between Int32 and int... =)
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Jan 21 '06 #7
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 21 '06 #8
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

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

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

"Lawrence Kevin" <sp******@spam.com> wrote in message
news:3a*************************@news.rmplc.co.uk. ..
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

Jan 23 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by S.Tobias | last post: by
31 posts views Thread by CeZaR | last post: by
4 posts views Thread by kgpsoftware | last post: by
9 posts views Thread by psujkov | last post: by
7 posts views Thread by =?Utf-8?B?Sm9obiBTdGFnZ3M=?= | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.