Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to auto convert from object or string to target property type. #92

Open
Korporal opened this issue Aug 25, 2021 · 1 comment

Comments

@Korporal
Copy link

Korporal commented Aug 25, 2021

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 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:

        public abstract string this[object target, string name]
        {
            get;
            set;
        }

or perhaps something like this:

        public abstract object this[object target, string name, bool autoconvert = false]
        {
            get;
            set;
        }

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?

@Cyril-hcj
Copy link

Are there any conclusions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants