I'm working with a problem in which I must set properties in some object from a string not from a compatibly typed expression.
For example this is valid now:
accessor[instance, "Quantity"] = 123.45M;
where the property Quantity is of type decimal.
but this throws:
accessor[instance, "Quantity"] = 123.45;
as does
accessor[instance, "Quantity"] = "123.45";
So I'd like to see if its feasible to extend this API so that an implicit call is made to Convert.ChangeType to attempt to convert the source to the target type automatically.
In my case it is usually a string source because I'm converting a set of CSV records into anIEnumerable<SomeClass> so in my code I set the property values from a text value found in the relevant CSV column.
Not sure how best you'd expose that but as the API developer that's best left to you, but it would be hugely helpful to be able to set properties from string values rather than from correctly typed source expression.
I'm working with a problem in which I must set properties in some object from a string not from a compatibly typed expression.
For example this is valid now:
where the property Quantity is of type
decimal
.but this throws:
as does
So I'd like to see if its feasible to extend this API so that an implicit call is made to
Convert.ChangeType
to attempt to convert the source to the target type automatically.In my case it is usually a string source because I'm converting a set of CSV records into an
IEnumerable<SomeClass>
so in my code I set the property values from a text value found in the relevant CSV column.Not sure how best you'd expose that but as the API developer that's best left to you, but it would be hugely helpful to be able to set properties from string values rather than from correctly typed source expression.
One idea is to add:
or perhaps something like this:
I also don't know if
Convert.ChangeType
is itself slow and might possibly negate the benefits of the API itself.What are your thoughts?