Convert String to Type in C#

Convert String to Type in C#

You can only use just the name of the type (with its namespace, of course) if the type is in mscorlib or the calling assembly. Otherwise, you've got to include the assembly name as well:

Type type = Type.GetType("Namespace.MyClass, MyAssembly");

If the assembly is strongly named, you've got to include all that information too. See the documentation for Type.GetType(string) for more information.

Alternatively, if you have a reference to the assembly already (e.g. through a well-known type) you can use Assembly.GetType:

Assembly asm = typeof(SomeKnownType).Assembly;
Type type = asm.GetType(namespaceQualifiedTypeName);

Convert string (System.String) to type

You need to convert the string System.String into the type System.String.

You can do that with Type.GetType(string typeName);

For example, the type variable below will have the Type object of System.String.

var type = Type.GetType("System.String");

You can then use that Type in the Convert.ChangeType overload you're using.

Convert.ChangeType(fieldValue, type);

Convert string to type of another variable

you can use
Convert.ChangeType(s,param.GetType())

https://msdn.microsoft.com/en-us/library/dtb69x08(v=vs.110).aspx

or

ConvertTo(s,param.GetType())

https://msdn.microsoft.com/en-us/library/y13battt(v=vs.110).aspx

Convert string to type in object

There is no absolute answer that works for all types. But, you could use a TypeConverter instead of Convert, it usually works better. For example, there is a TimeSpanConverter:

public void Update<T>(string fieldName, string fieldValue)
{
System.Reflection.PropertyInfo propertyInfo = typeof(T).GetProperty(fieldName);
Type propertyType = propertyInfo.PropertyType;

TypeConverter converter = TypeDescriptor.GetConverter(type);
if (converter.CanConvertFrom(typeof(string)))
{
var a = converter.ConvertFrom(fieldValue, type);
...
}
}

How to convert string to any type


using System.ComponentModel;

TypeConverter typeConverter = TypeDescriptor.GetConverter(propType);
object propValue = typeConverter.ConvertFromString(inputValue);

Convert String to Type unknown at compile time

I managed to achieve what I wanted using the following:

public bool DataIsValid(string s, Type someType) {
o = 0; // a class member object variable

try { o = Convert.ChangeType(s, someType); }
catch { return false; }

return true;
}

//using the object variable
double someDouble = (double)0;
//some more code

Thanks for the help.

Get property Type and convert String to corresponding Type

I use TypeDescriptor.GetConverter that takes a Type and returns a TypeConverter that knows how to convert a string to the specified Type.

You can then call TypeConverter.ConvertFromString to convert the string to the required Type.

Your code would look like:

var propType = src.GetType().GetProperty(propName).PropertyType;
var converter = TypeDescriptor.GetConverter(propType);
var convertedObject = converter.ConvertFromString(src);

Convert string to generic type c# (Convert string to T)

you can use Convert.ChangeType

T GetValue<T>(string name)
{
string item = getstuff(name);
return (T)Convert.ChangeType(item, typeof(T));
}

if you need to limit input types only for int and DateTime, add condition like below

if (typeof(T) != typeof(int) && typeof(T) != typeof(DateTime))
{
// do something with other types
}

Given a Type and a String, can you try to convert the String to that Type without interogating the Type?

Convert.ChangeType() will parse strings to numbers when possible, throwing an exception on strings that aren't valid numbers.

Note: If you are going to convert between your own custom types a lot, you may want to use a TypeConverter instead. (TypeConverter will work better when converting both to-from primitive types like string.)

See:
Convert System.String generically to any complex type using "Convert.ChangeType()"

EDIT:
Yes, Convert.ChangeType("1.02", typeof(double)) is double should evaluate to true assuming no exceptions are thrown.

Convert Strings to Type Instances


var conv = TypeDescriptor.GetConverter(type);
return conv.ConvertFromInvariantString(value);

Other conversion operations exist if you don't want "invariant". It depends on your needs. See also ConvertFromString if you want locale settins to apply, etc.



Related Topics



Leave a reply



Submit