By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,953 Members | 1,562 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,953 IT Pros & Developers. It's quick & easy.

Converting form C# alias type to .NET framework type

P: n/a
There is an built-in types table for .NET framework type to C# type which are
aliases of predefined types in the System namespace
(http://msdn2.microsoft.com/en-US/library/ya5y69ds.aspx).

How can use access and use this table (by C# code) to convert the alias to
the .NET framework type.

For example: if I have:
string aliasType = "int";
string frameworkType = ???
-------
Thanks
Sharon
Nov 1 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hi Sharon,

I'm not exactly sure how to do it using CodeDom, although I suspect that it's
possible, but here's a custom solution that should work just fine (2.0
framework):

System.Collections.Generic.Dictionary<string, stringcSharpTokens =
new System.Collections.Generic.Dictionary<string, string>();

cSharpTokens.Add("bool", "System.Boolean");
cSharpTokens.Add("byte", "System.Byte");
cSharpTokens.Add("sbyte", "System.SByte");
cSharpTokens.Add("char", "System.Char");
cSharpTokens.Add("decimal", "System.Decimal");
cSharpTokens.Add("double", "System.Double");
cSharpTokens.Add("float", "System.Single");
cSharpTokens.Add("int", "System.Int32");
cSharpTokens.Add("uint", "System.UInt32");
cSharpTokens.Add("long", "System.Int64");
cSharpTokens.Add("ulong", "System.UInt64");
cSharpTokens.Add("object", "System.Object");
cSharpTokens.Add("short", "System.Int16");
cSharpTokens.Add("ushort", "System.UInt16");
cSharpTokens.Add("string", "System.String");

Type type = Type.GetType(cSharpTokens["int"]);

Console.WriteLine(type.FullName);

Output:

System.Int32

--
Dave Sexton

"Sharon" <Sh*****@newsgroups.nospamwrote in message
news:E6**********************************@microsof t.com...
There is an built-in types table for .NET framework type to C# type which
are
aliases of predefined types in the System namespace
(http://msdn2.microsoft.com/en-US/library/ya5y69ds.aspx).

How can use access and use this table (by C# code) to convert the alias to
the .NET framework type.

For example: if I have:
string aliasType = "int";
string frameworkType = ???
-------
Thanks
Sharon

Nov 1 '06 #2

P: n/a
I'm also convinced this will work.
But I'm using .NET framework 1.1 (I can use any hashtable in 1.1 as well)
and this solution does not use the built-in table.

The trick is to work with the .NET framework built-in table.

There must be a way to it. No ?!
--
Thanks
Sharon
Nov 1 '06 #3

P: n/a
Hi Sharon,

There is no built-in table as far as I'm concerned. That information is
proprietary to the compiler.

As I said, CodeDom may offer some mechanism to serialize C# tokens but I
wouldn't recommend that over hard-coding the Hashtable anyway.

Are you worried that they're going to change the tokens in the next release of
the framework? ;)

--
Dave Sexton

"Sharon" <Sh*****@newsgroups.nospamwrote in message
news:3D**********************************@microsof t.com...
I'm also convinced this will work.
But I'm using .NET framework 1.1 (I can use any hashtable in 1.1 as well)
and this solution does not use the built-in table.

The trick is to work with the .NET framework built-in table.

There must be a way to it. No ?!
--
Thanks
Sharon

Nov 1 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.