Obtaining c# type declaration

  • Thread starter Thread starter Lawrence Kevin
  • Start date Start date
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
 
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
 
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
 
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.
 
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 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.
 
Well, a statement could be made about one who doesn't know the
difference between Int32 and int... =)
 
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.
 
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.
 
I'm sticking to the C# keywords purely to follow coding-standards within
our department.

Kev
 
Back
Top