投射反射对象不起作用 [英] Cast reflected objects doesn't work

查看:85
本文介绍了投射反射对象不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨专家!

我有一点问题。

我想操纵一个反射的对象,但是我太愚蠢了,无法转换数据类型:(

请帮帮我:)

这是我的想法:

但它不起作用









Hi Experts!
I have a little problem.
I want to manipulate a reflected object, but i am too stupid to cast the datatype :(
Please help me :)
This is my idea:
But it doesn't work




myObjectFromDB = GetFromDB(); // Get the object via Entity Framework form DB

for(...)
{

						//item.Key = myPropertyName;
						//item.Value = true (as string)
						
						// The property 'myPropertyName' is in my DB a nullable bool
						

                        PropertyInfo propInfo = typeof(myObject).GetProperty(item.Key);

                        dynamic property = propInfo.GetValue(myObjectFromDB, null);
                        dynamic propertyType = property.GetType();

                        dynamic newVal = item.Value;

                        Convert.ChangeType(newVal, propertyType);

                        propInfo.SetValue(myObjectFromDB, newVal);
						// The  propInfo.SetValue throws the error!
						// Error: Das Objekt mit dem Typ "System.String" kann nicht in den Typ "System.Nullable`1[System.Boolean]" konvertiert werden. 
						//-> Can´t convert the Object from String to Boolean
						
}





我尝试过:



一些愚蠢的东西,但它不起作用



What I have tried:

some some stupid stuff, But it doesn't work

推荐答案

Quote:

Convert.ChangeType(newVal,propertyType);

propInfo.SetValue(myObjectFromDB,newVal);

Convert.ChangeType(newVal, propertyType);
propInfo.SetValue(myObjectFromDB, newVal);



有你的问题 - Convert.ChangeType 返回转换结果。它不会改变你传入的对象的类型。



你还需要使用 property.PropertyType 而不是 property.GetType()。前者返回属性的声明类型;后者将返回 typeof(PropertyInfo)



编辑:看起来像你'转换也会出现问题 - ChangeType 无法应对将字符串转换为可空类型,并为您提供 InvalidCastException 。你需要转换为基础类型。



尝试将代码更改为:


There's your problem - Convert.ChangeType returns the result of the conversion. It doesn't change the type of the object you've passed in.

You'll also need to use property.PropertyType instead of property.GetType(). The former returns the declared type of the property; the latter will return typeof(PropertyInfo).

Looks like you're also going to have a problem with the conversion - ChangeType can't cope with converting a string to a nullable type, and gives you an InvalidCastException. You'll need to convert to the underlying type instead.

Try changing your code to:

Type propertyType = property.PropertyType;
Type realPropertyType = Nullable.GetUnderlyingType(propertyType) ?? propertyType;
object newPropertyValue = Convert.ChangeType(newVal, realPropertyType);
propInfo.SetValue(myObjectFromDB, newPropertyValue);


您要在其中强制转换的Object必须声明为应该作为Destination的类型...

某些部分无法变量......必须指定它们(此类型)...
The Object in which you want to cast must be declared as the type which should be the Destination ...
Some parts could not be variable ... they (the type from this) must be specified ...


理查德,谢谢! !



昨天,经过10个小时的工作是我的大脑,就像一块柔软的饼干;)



new一天,新运气:)



非常感谢所有读过这个主题的人



br,



Benny
Richard, THANK YOU!!!

Yesterday, after 10 hours working was my brain, like a limp biscuit ;)

new day, new luck :)

Many Thanks to everyone who has read this thread

br,

Benny


这篇关于投射反射对象不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆