I have this bit of code
NullableConverter nullableConverter = new NullableConverter(type);
Type baseType = nullableConverter.UnderlyingType;
Object value = Convert.ChangeType(rule.Data, baseType);
constant = Expression.Constant(value);
In this case type is decimal? but it could be any nullable type.
I need value to be of type decimal?, not type decimal as it currently is. The problem, is that Convert.ChangeType doesn’t work properly with nullable types. So I’m creating a non-nullable first.
How can I convert the string contained in rule.Data to type?
You can’t.
When a nullable struct is boxed in an
Objectit doesn’t store a boxed nullable struct. If the struct doesn’t have a value it will just boxnull, and if it has a value it will box the underlying value of the nullable struct.You would need to not be boxing it for this to even be possible.