Skip to content

Provide Default Type Converter for Quanitities #757

@WalkerCodeRanger

Description

@WalkerCodeRanger

Is your feature request related to a problem? Please describe.
The QuantityTypeConverter<TQuantity> class included with UnitsNet provides a type converter that can be used, for example for Length, by applying the attribute [TypeConverter(typeof(QuantityTypeConverter<Length>))] to each property that requires type conversion. However, this can be a lot of properties and is easy to forget to do.

Describe the solution you'd like
Typically, when a class is distributed with a type converter, it is associated with the declaration of the class so that the type converter will be picked up by default whenever one is needed. The type converter can still be overridden by applying a type converter attribute to the property. Following the usual convention, I think all quantity types should have a type converter applied to their class using the TypeConverterAttribute.

Describe alternatives you've considered
Users of the UnitsNet library can work around this using one of two techniques:

  1. Apply a TypeConverter attribute to each property that requires type conversion.
  2. Registering a default type converter using TypeDescriptor.AddAttributes(typeof(TQuantity), new TypeConverterAttribute(typeof(QuantityTypeConverter<TQuantity>)));. Note that this must be done very early in the application to ensure the registration is done before the type converter is requested using TypeDescriptor.GetConverter(typeof(TQuantity)).

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions