need a typeof that take a string description of the type

A

andrew.ropson

I need to generate a Type instance from a string decription but not
the System.<typename> the raw type name so Type.GetType() won't work.

e.g.

string strTypename = "short"; // in practice this is read from a
database

Type shortType1 = typeof(short);
Type shortType2 = Type.GetType("System.Int16"); // if there is a
function to go from "short" to "System.Int16"
// this is what I want
Type dynamicType = typeofFromName(strTypeName); // ability to get the
type based on the true type name


I could of course write my own conversion function, but before I do
that, is there anything that does this already?

Thanks
 
A

andrew.ropson

I need to generate a Type instance from a string decription but not
the System.<typename> the raw type name so Type.GetType() won't work.

e.g.

string strTypename = "short"; // in practice this is read from a
database

Type shortType1 = typeof(short);
Type shortType2 = Type.GetType("System.Int16"); // if there is a
function to go from "short" to "System.Int16"
// this is what I want
Type dynamicType = typeofFromName(strTypeName); // ability to get the
type based on the true type name

I could of course write my own conversion function, but before I do
that, is there anything that does this already?

Thanks

To make this clearer I want to do this:

private Type TypeOfByName(string strName) {
Type type = null;

switch (strName) {
case "string":
type = typeof(string);
break;
case "bool":
type = typeof(bool);
break;
case "byte":
type = typeof(byte);
break;
case "char":
type = typeof(char);
break;
case "decimal":
type = typeof(decimal);
break;
case "double":
type = typeof(double);
break;
case "float":
type = typeof(float);
break;
case "int":
type = typeof(int);
break;
case "long":
type = typeof(long);
break;
case "sbyte":
type = typeof(sbyte);
break;
case "short":
type = typeof(short);
break;
case "uint":
type = typeof(uint);
break;
case "ulong":
type = typeof(ulong);
break;
case "ushort":
type = typeof(ushort);
break;
}

return type;
}

but without a big switch
 
A

andrew.ropson

Hm. The only thing I can really suggest is to do a mapping internally
of
the names you have to the C# types you want them represented by. Other
than
that, there is really no way for the compiler to "guess" at what type
you
want.

Matt

Thanks, Matt but I may aswell stick with the switch pattern as do
that, it works but does feel very elegant.
 
A

andrew.ropson

To make this clearer  I want to do this:
[...]
but without a big switch

Well, you could use a pre-initialized Dictionary instead.  :)

But, no...I don't think there's a simple programmatic way to translate  
 from those strings to the type name.  The source names are aliases  
understood by C#, not the CLR.  The only part of the CLR that would know  
them is the compiler services part.  You could, of course, use those  
classes to compile code with the names of interest in it and then inspect 
the code to see what type fell out.  But that would be awkward at  
best...just hard-coding the mapping in your code is probably sufficient,  
given that the mappings are almost certainly never going to change.

Pete

Thanks for your comments, a dictionary would be a bit more tidy. I've
since discovered that the types are not c# types but some other types
used by xmlserializing presumabley soap or some other xml standard,
this caused me a problem because I was expecting C# type 'bool' but
got back the type 'boolean' instead
 

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