Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Given PropertyInfo.PropertyType how to cast PropertyInfo.GetValue() to that type

PropertyInfo.GetValue() returns an object. I need to cast that object to the type returned from PropertyInfo.PropertyType. How can I do it? The only way I think is a switch on PropertyType.ToString(). Is there another way? TIA

like image 422
marcelo Avatar asked Aug 31 '25 03:08

marcelo


1 Answers

It IS possible. With the PropertyInfo Value and PropertyType you can: var typedPropertyVal = Convert.ChangeType(propertyVal, propertyType); This is the post that helped me: technico.qnownow.com/reflection-setvalue-type-conversion

like image 137
Ryan Van Der Wal Avatar answered Sep 02 '25 18:09

Ryan Van Der Wal