Click here to Skip to main content
15,867,986 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
Posted
Updated 15-Feb-17 22:18pm
Comments
StM0n 15-Feb-17 4:50am    
First of all, why are you using dynamic? Imho it's not really necessary... at least form my point of view, but I could be wrong.

Guess the value you're trying to convert is not really convertible... :) do you getting a "Falsch" or "Wahr" through your DB?
BoySetsFire 15-Feb-17 4:56am    
Hi & Thx for your answer... the dynamic comes from testing... if i cast by hand is works like convert.tobool(val)

the datatype in the db is a bit

br,
Benny
StM0n 15-Feb-17 7:47am    
So maybe you should do some tests with the conversion to obtain the right info :)

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

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).

EDIT: 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);
 
Share this answer
 
v3
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 ...
 
Share this answer
 
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900