我已经扩展了这篇文章中给出的单位转换器类: https ://stackoverflow.com/a/7852721/1474894 这就是我现在所拥有的:
public abstract class UnitBase<TUnitType, TValueType> where TUnitType : struct, IComparable, IConvertible, IFormattable
{
protected static TUnitType BaseUnit;
protected static TValueType BaseValue;
private static ConcurrentDictionary<TUnitType, Func<TValueType, TValueType>> ConversionsTo = new ConcurrentDictionary<TUnitType, Func<TValueType, TValueType>>();
private static ConcurrentDictionary<TUnitType, Func<TValueType, TValueType>> ConversionsFrom = new ConcurrentDictionary<TUnitType, Func<TValueType, TValueType>>();
public static TValueType Convert(TValueType value, TUnitType from, TUnitType to)
{
// If both From/To are the same, don't do any work.
if (from.Equals(to))
return value;
// Convert into the base unit, if required.
var valueInBaseUnit = from.Equals(BaseUnit)
? value
: ConversionsFrom[from](value);
// Convert from the base unit into the requested unit, if required
var valueInRequiredUnit = to.Equals(BaseUnit)
? valueInBaseUnit
: ConversionsTo[to](valueInBaseUnit);
return valueInRequiredUnit;
}
protected static void RegisterConversion(TUnitType convertToUnit, Func<TValueType, TValueType> conversionTo, Func<TValueType, TValueType> conversionFrom)
{
if (!ConversionsTo.TryAdd(convertToUnit, conversionTo))
throw new ArgumentException("Already exists", "convertToUnit");
if (!ConversionsFrom.TryAdd(convertToUnit, conversionFrom))
throw new ArgumentException("Already exists", "convertToUnit");
}
public static string GetUnit(TUnitType unit)
{
var type = typeof(TUnitType);
if (!type.IsEnum)
{
throw new ArgumentException();
}
return Enums.GetEnumDescription(unit as Enum);
}
public TValueType InUnit(TUnitType unit)
{
return Convert(BaseValue, BaseUnit, unit);
}
}
我有这样的具体实现:
public enum TemperatureUnit
{
[System.ComponentModel.Description("°C")]
Celcius,
[System.ComponentModel.Description("°F")]
Fahrenheit
}
public class Temperature : UnitBase<TemperatureUnit, double>
{
static Temperature()
{
BaseUnit = TemperatureUnit.Celcius;
RegisterConversion(TemperatureUnit.Fahrenheit, x => x * 1.8d + 32d, x => (x - 32d) / 1.8d);
}
private Temperature(double value)
{
BaseValue = value;
}
public static Temperature FromUnit(double value, TemperatureUnit unit)
{
return new Temperature(Convert(value, unit, BaseUnit));
}
}
我设法将 ToUnit 方法作为基类的一部分进行通用化,但是使用 FromUnit 方法,我现在每个具体类都有一个(它们看起来都非常相似)。我希望能够做到这一点:
Temperature temperature = Temperature.FromUnit(10, TemperatureUnit.Celcius);
Pressure pressure = Pressure.FromUnit(50, PressureUnit.Bar);
...无需在每个具体类中实现 FromUnit。因为它是一种静态方法,所以我不能通过使用接口来强制实现。我可以在具体类中有一个公共构造函数,并且有类似的东西:
public static T FromUnit<T>(TValueType value, TUnitType unit) where T : new()
{
BaseValue = Convert(value, unit, BaseUnit);
return new T();
}
但我想它不是那么好,我将不得不去:
Temperature temperature = Temperature.FromUnit<Temperature>(10, TemperatureUnit.Celcius);
我认为这<Temperature>
是多余的。
是否有可能以更好的方式实现通用 FromUnit?
编辑: 基本上我想要,而不是在每个子类中实现 FromUnit:
public static Temperature FromUnit(double value, TemperatureUnit unit)
{
return new Temperature(Convert(value, unit, BaseUnit));
}
public static Pressure FromUnit(double value, PressureUnit unit)
{
return new Pressure(Convert(value, unit, BaseUnit));
}
public static Speed FromUnit(double value, SpeedUnit unit)
{
return new Speed(Convert(value, unit, BaseUnit));
}
...
...将通用 FromUnit 放在基类中。