As WCF does not support Types, I'm passing the type as a string type. For eg:
var str= "int"
Now I want to convert this to a Type int as i want to pass CLR types as parameters.
Is there anyway of achieving this?
As WCF does not support Types, I'm passing the type as a string type. For eg:
var str= "int"
Now I want to convert this to a Type int as i want to pass CLR types as parameters.
Is there anyway of achieving this?
You mean like using Type.GetType()?
string typeName = "System.Int32"; // Sadly this won't work with just "int"
Type actualType = Type.GetType(typeName);
If the type is in the currently executing assembly or in Mscorlib.dll, it is sufficient to get the type name qualified by its namespace (see @Rawling answer):
var str = typeof(int).FullName;
// str == "System.Int32"
Otherwise, you need an assembly-qualified name of the Type
:
var str = typeof(int).AssemblyQualifiedName;
// str == "System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
Then you can use Type.GetType
:
var intType = Type.GetType(str);
Edit:
If you want to use the system aliases, you can create a Dictionary<string, Type>
to map all of the aliases to their type:
static readonly Dictionary<string, Type> Aliases =
new Dictionary<string, Type>()
{
{ "byte", typeof(byte) },
{ "sbyte", typeof(sbyte) },
{ "short", typeof(short) },
{ "ushort", typeof(ushort) },
{ "int", typeof(int) },
{ "uint", typeof(uint) },
{ "long", typeof(long) },
{ "ulong", typeof(ulong) },
{ "float", typeof(float) },
{ "double", typeof(double) },
{ "decimal", typeof(decimal) },
{ "object", typeof(object) }
};
Try this
int myInt = 0;
int.TryParse(str, out myInt);
if(myInt > 0)
{
// do your stuff here
}
if you mean you only want to sent the type then use
string str = myInt.GetType().ToString();
and it will give you the type