通过反射使用字符串值设置属性

本文翻译自:Setting a property by reflection with a string value

I'd like to set a property of an object through Reflection, with a value of type string . 我想通过Reflection设置对象的属性,其值为string类型。 So, for instance, suppose I have a Ship class, with a property of Latitude , which is a double . 因此,例如,假设我有一个Ship类,其属性为Latitude ,这是一个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 : 原样,这会抛出ArgumentException

Object of type 'System.String' cannot be converted to type 'System.Double'. “System.String”类型的对象无法转换为“System.Double”类型。

How can I convert value to the proper type, based on propertyInfo ? 如何根据propertyInfo将值转换为正确的类型?


#1楼

参考:https://stackoom.com/question/4ZKV/通过反射使用字符串值设置属性


#2楼

As several others have said, you want to use Convert.ChangeType : 正如其他几个人所说,你想使用Convert.ChangeType

propertyInfo.SetValue(ship,
    Convert.ChangeType(value, propertyInfo.PropertyType),
    null);

In fact, I recommend you look at the entire Convert Class . 实际上,我建议你看看整个Convert

This class, and many other useful classes are part of the System Namespace . 此类和许多其他有用的类是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! 试试看!


#3楼

You can use Convert.ChangeType() - It allows you to use runtime information on any IConvertible type to change representation formats. 您可以使用Convert.ChangeType() - 它允许您使用任何IConvertible类型的运行时信息来更改表示格式。 Not all conversions are possible, though, and you may need to write special case logic if you want to support conversions from types that are not IConvertible . 但是,并非所有转换都是可能的,如果您希望支持非IConvertible类型的转换,则可能需要编写特殊情况逻辑。

The corresponding code (without exception handling or special case logic) would be: 相应的代码(无异常处理或特殊情况逻辑)将是:

Ship ship = new Ship();
string value = "5.5";
PropertyInfo propertyInfo = ship.GetType().GetProperty("Latitude");
propertyInfo.SetValue(ship, Convert.ChangeType(value, propertyInfo.PropertyType), null);

#4楼

Or you could try: 或者你可以尝试:

propertyInfo.SetValue(ship, Convert.ChangeType(value, propertyInfo.PropertyType), null);

//But this will cause problems if your string value IsNullOrEmplty...

#5楼

You're probably looking for the Convert.ChangeType method. 您可能正在寻找Convert.ChangeType方法。 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);

#6楼

Using Convert.ChangeType and getting the type to convert from the PropertyInfo.PropertyType . 使用Convert.ChangeType并从PropertyInfo.PropertyType获取要转换的类型。

propertyInfo.SetValue( ship,
                       Convert.ChangeType( value, propertyInfo.PropertyType ),
                       null );
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值