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

InvalidCastException when ConvertToModel converts from a list to a non-list #68

Open
GrantShotwell opened this issue Aug 3, 2023 · 0 comments
Labels
bug Something isn't working PR welcome User contribution/PR is welcome

Comments

@GrantShotwell
Copy link

Exception appears to be thrown in SyntaxToModelTransform.GetOrCreateSubObject(...), line 216.

The reason I am getting this exception is because I am trying to use wrappers for various types, and so implemented a ConvertToModel that creates the wrapper from the deserialized value. The error happens when the wrapped type is a list.

If it helps, the ConvertToModel function looks like this:

interface IWrapper<T> { T Value { get; } }
class StructValue<T> : IWrapper<T> where T : struct { /* ... */ }
class ReferenceValue<T> : IWrapper<T> where T : class { /* ... */ }
class ListValue<T> : IWrapper<IList<T>> { /* ... */ }
// ... and others

object? ConvertToModel(object deserialized, Type targetType) {
    // [return deserialized if deserialized.GetType() is already targetType]
    // [return null if targetType does not implement IWrapper]
    return CreateWrapper(deserialized, targetType);
}

This works for StructValue and ReferenceValue, just not ListValue because of the error.

@xoofx xoofx added bug Something isn't working PR welcome User contribution/PR is welcome labels Aug 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working PR welcome User contribution/PR is welcome
Projects
None yet
Development

No branches or pull requests

2 participants