我扩展了本文中给出的单位转换器类:
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放在基类中。
最佳答案
您的“不太好的”解决方案中的<Temperature>
类型参数似乎是多余的,但实际上不是多余的。当你写
Temperature temperature = Temperature.FromUnit<Temperature>(10, TemperatureUnit.Celcius);
该代码实际上被翻译成
Temperature temperature = UnitBase<TemperatureUnit, double>.FromUnit<Temperature>(10, TemperatureUnit.Celcius);
编译器非常好,足以允许您使用
Temperature
访问该方法。由于它实际上是在调用UnitBase.FromUnit()
,因此不知道您实际上要使用Temperature
,这就是为什么必须将其作为附加类型参数提供的原因。