Converting form C# alias type to .NET framework type

G

Guest

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 = ???
 
D

Dave Sexton

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, string> cSharpTokens =
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
 
G

Guest

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 ?!
 
D

Dave Sexton

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? ;)
 

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