通过反射设置属性时的类型转换问题

编程入门 行业动态 更新时间:2024-10-10 15:17:40
本文介绍了通过反射设置属性时的类型转换问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我们有一个类型为 long?的属性,其中填充了 int 。

We have a property of type long? that gets filled with an int.

当我直接将属性设置为 obj.Value = v; 时,此方法很好,但是当我尝试通过反射 info.SetValue(obj,v,null); 它给了我一个以下异常:

This works fine when i just set the property directly obj.Value = v; but when i try and set the property through reflection info.SetValue(obj, v, null); it gives me a the following exception:

类型'System.Int32'的对象不能转换为类型'System.Nullable`1 [System.Int64]'。

Object of type 'System.Int32' cannot be converted to type 'System.Nullable`1[System.Int64]'.

这是一个简化的场景:

class TestClass { public long? Value { get; set; } } [TestMethod] public void TestMethod2() { TestClass obj = new TestClass(); Type t = obj.GetType(); PropertyInfo info = t.GetProperty("Value"); int v = 1; // This works obj.Value = v; // This does not work info.SetValue(obj, v, null); }

为什么通过设置属性时不起作用反射在直接设置属性时有效吗?

Why does it not work when setting the property through reflection while it works when setting the property directly?

推荐答案

查看全文:如何使用反射来设置属性的值?

完整代码,如果您要为可空类型设置值

full code if you are setting value for nullable type

public static void SetValue(object inputObject, string propertyName, object propertyVal) { //find out the type Type type = inputObject.GetType(); //get the property information based on the type System.Reflection.PropertyInfo propertyInfo = type.GetProperty(propertyName); //find the property type Type propertyType = propertyInfo.PropertyType; //Convert.ChangeType does not handle conversion to nullable types //if the property type is nullable, we need to get the underlying type of the property var targetType = IsNullableType(propertyType) ? Nullable.GetUnderlyingType(propertyType) : propertyType; //Returns an System.Object with the specified System.Type and whose value is //equivalent to the specified object. propertyVal = Convert.ChangeType(propertyVal, targetType); //Set the value of the property propertyInfo.SetValue(inputObject, propertyVal, null); } private static bool IsNullableType(Type type) { return type.IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(Nullable<>)); }

您需要转换价值这样,即您需要将值转换为如下所示的属性类型

you need to convert value like this i.e you need to convert value to your property type like as below

PropertyInfo info = t.GetProperty("Value"); object value = null; try { value = System.Convert.ChangeType(123, Nullable.GetUnderlyingType(info.PropertyType)); } catch (InvalidCastException) { return; } propertyInfo.SetValue(obj, value, null);

您需要执行此操作,因为您无法将任意值转换为给定类型...因此您需要像这样转换

you need to do this because you cannot convert any arbirtary value to given type...so you need to convert it like this

更多推荐

通过反射设置属性时的类型转换问题

本文发布于:2023-11-09 03:55:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1571331.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:反射   属性   类型

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!