views:

81

answers:

1

Hi there:

So the problem is I have the following line wehre value is a string

var filterValue = Expression.Constant(value, property.Type);

if property.Type is a string everything is fine however the type really could be anything, like a decimal?

I dont know how to make this work for all different types I have this function

private static T Parse (string value)
{
    return (T)TypeDescriptor.GetConverter(typeof(T)).ConvertFromString(value);
}

using that I can do this:

var newValue = Parse(value);
var filterValue = Expression.Constant(newValue, property.Type);

however I would have to know in advance the type , I tried

var newValue = Parse(value);

but that doesnt work

Any ideas?

+1  A: 

You don't need to know the type at all:

object value = TypeDescriptor.GetConverter(property.Type).ConvertFromString(value);
var filterValue = Expression.Constant(value, property.Type);

The generics approach isn't appropriate (or needed) in this case.

You might want special handling for null, though - or simply not allow it (property.Type) could be int? etc...

Marc Gravell