简体   繁体   中英

Cast from generic class property

I want to cast from generic class of the propertyType and dont know how to do it.

I want to do something like this:

var key = Expression.Property(genericType, rule.ComparisonPredicate);
Type propertyType = typeof(T).GetProperty(rule.ComparisonPredicate).PropertyType;                                              
var converter = TypeDescriptor.GetConverter(propertyType);
var value = Expression.Constant((propertyType)converter.ConvertFromString(rule.ComparisonValue) );

but in the line var value i got an error :

'propertyType' is a variable but is used like a type

You cannot cast to a variable, You should specify a real type

var value = Expression.Constant(( propertyType )converter.ConvertFromString(rule.ComparisonValue) );

Try to cast the property type

(Type) typeof(T).GetProperty(rule.ComparisonPredicate).PropertyType

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM