I'd like to set a property of an object through Reflection, with a value of type string
.
So, for instance, suppose I have a Ship
class, with a property of Latitude
, which is a double
.
Here's what I'd like to do:
Ship ship = new Ship();
string value = "5.5";
PropertyInfo propertyInfo = ship.GetType().GetProperty("Latitude");
propertyInfo.SetValue(ship, value, null);
As is, this throws an ArgumentException
:
Object of type 'System.String' cannot be converted to type 'System.Double'.
How can I convert value to the proper type, based on propertyInfo
?
This question is related to
c#
reflection
type-conversion
propertyinfo
setvalue
You can use a type converter (no error checking):
Ship ship = new Ship();
string value = "5.5";
var property = ship.GetType().GetProperty("Latitude");
var convertedValue = property.Converter.ConvertFrom(value);
property.SetValue(self, convertedValue);
In terms of organizing the code, you could create a kind-of mixin that would result in code like this:
Ship ship = new Ship();
ship.SetPropertyAsString("Latitude", "5.5");
This would be achieved with this code:
public interface MPropertyAsStringSettable { }
public static class PropertyAsStringSettable {
public static void SetPropertyAsString(
this MPropertyAsStringSettable self, string propertyName, string value) {
var property = TypeDescriptor.GetProperties(self)[propertyName];
var convertedValue = property.Converter.ConvertFrom(value);
property.SetValue(self, convertedValue);
}
}
public class Ship : MPropertyAsStringSettable {
public double Latitude { get; set; }
// ...
}
MPropertyAsStringSettable
can be reused for many different classes.
You can also create your own custom type converters to attach to your properties or classes:
public class Ship : MPropertyAsStringSettable {
public Latitude Latitude { get; set; }
// ...
}
[TypeConverter(typeof(LatitudeConverter))]
public class Latitude { ... }
As several others have said, you want to use Convert.ChangeType
:
propertyInfo.SetValue(ship,
Convert.ChangeType(value, propertyInfo.PropertyType),
null);
In fact, I recommend you look at the entire Convert
Class.
This class, and many other useful classes are part of the System
Namespace. I find it useful to scan that namespace every year or so to see what features I've missed. Give it a try!
I notice a lot of people are recommending Convert.ChangeType
- This does work for some cases however as soon as you start involving nullable
types you will start receiving InvalidCastExceptions
:
A wrapper was written a few years ago to handle this but that isn't perfect either.
Using Convert.ChangeType
and getting the type to convert from the PropertyInfo.PropertyType
.
propertyInfo.SetValue( ship,
Convert.ChangeType( value, propertyInfo.PropertyType ),
null );
I tried the answer from LBushkin and it worked great, but it won't work for null values and nullable fields. So I've changed it to this:
propertyName= "Latitude";
PropertyInfo propertyInfo = ship.GetType().GetProperty(propertyName);
if (propertyInfo != null)
{
Type t = Nullable.GetUnderlyingType(propertyInfo.PropertyType) ?? propertyInfo.PropertyType;
object safeValue = (value == null) ? null : Convert.ChangeType(value, t);
propertyInfo.SetValue(ship, safeValue, null);
}
Are you looking to play around with Reflection or are you looking to build a production piece of software? I would question why you're using reflection to set a property.
Double new_latitude;
Double.TryParse (value, out new_latitude);
ship.Latitude = new_latitude;
Using the following code should solve your issue:
item.SetProperty(prop.Name, Convert.ChangeType(item.GetProperty(prop.Name).ToString().Trim(), prop.PropertyType));
Or you could try:
propertyInfo.SetValue(ship, Convert.ChangeType(value, propertyInfo.PropertyType), null);
//But this will cause problems if your string value IsNullOrEmplty...
If you are writing Metro app, you should use other code:
Ship ship = new Ship();
string value = "5.5";
PropertyInfo propertyInfo = ship.GetType().GetTypeInfo().GetDeclaredProperty("Latitude");
propertyInfo.SetValue(ship, Convert.ChangeType(value, propertyInfo.PropertyType));
Note:
ship.GetType().GetTypeInfo().GetDeclaredProperty("Latitude");
instead of
ship.GetType().GetProperty("Latitude");
I will answer this with a general answer. Usually these answers not working with guids. Here is a working version with guids too.
var stringVal="6e3ba183-89d9-e611-80c2-00155dcfb231"; // guid value as string to set
var prop = obj.GetType().GetProperty("FooGuidProperty"); // property to be setted
var propType = prop.PropertyType;
// var will be type of guid here
var valWithRealType = TypeDescriptor.GetConverter(propType).ConvertFrom(stringVal);
You're probably looking for the Convert.ChangeType
method. For example:
Ship ship = new Ship();
string value = "5.5";
PropertyInfo propertyInfo = ship.GetType().GetProperty("Latitude");
propertyInfo.SetValue(ship, Convert.ChangeType(value, propertyInfo.PropertyType), null);
Source: Stackoverflow.com