I have a simple model class that holds values from a calculation, as doubles. Several properties are accessible, and I control the base unit, in this case I can guarantee they are in Millimeters for length, cubic millimeters for Volume and so on.
public class Model
{
[MyDisplay(UnitsNet.Volume)]
public double Volume { get; set; }
[MyDisplay(UnitsNet.Length)]
public double Length { get; set; }
}
I would like to annotate the properties with their type, so that in a display application, the user can choose the appropriate unit, i.e. all lengths in inches, all volumes in cubic feet. This display is configured at runtime, and I would like to offer the user a choice what unit they want to display the values in, based on the attribute.
What would the MyDisplayAttribute
class look like? At runtime, I can get all the properties on my Model
class, get the value of the attribute and convert the model value to the user chosen unit, but I'm lost on how to do that.