我有一些定义测量信息的枚举,如体积、质量、距离等.
它们的定义如下:
/// <summary>
/// Units for measurement of area.
/// </summary>
public enum Area
{
/// <summary>
/// Meters squared.
/// </summary>
M2 = 0,
/// <summary>
/// Feet squared.
/// </summary>
FT2
}我有必要显示一个简短和长的描述单位。在我的例子中,对仪表的简短描述将是[m],而long将简单地称为Meters。还有其他更大的枚举,用于定义Kilometers per hour per second/[km/h/s]的加速。
到目前为止,我所做的是使用DescriptionAttribute属性来提供简短的描述:
/// <summary>
/// Distance measurement units
/// </summary>
public enum Distance
{
/// <summary>
/// Meters.
///</summary>
[Description("[m]")]
M = 0,
...
}这已经适用于有一种Distance类型的东西。现在我得到了定义为整数类型的东西,它们实际上是一种距离度量:
public class MovementSnapshot
{
/// <summary>
/// The total distance traveled, in meters.
/// </summary>
public int TotalTravelDistance { get; private set; }
/// <summary>
/// The current speed of the car. In KM/H
/// </summary>
public int Speed { get; private set; }
/// <summary>
/// The maximum speed achieved. In KM/H
/// </summary>
public int MaxSpeed { get; private set; }
}我定义了一个自定义属性,以便在将这些属性输出到文件/屏幕等时更容易地显示这些属性。看起来是这样的:
/// <summary>
/// Provides an interface for the display of Unit-type objects
/// </summary>
public class UnitDisplayAttribute
: Attribute
{
/// <summary>
/// The short display of the unit. Example: [N]
/// </summary>
public string ShortDisplay { get; private set; }
/// <summary>
/// Long long display of the unit. Example: Newtons
/// </summary>
public string LongDisplay { get; private set; }
/// <summary>
/// Construct the unit display attribute providing a short and long description of a unit type.
/// </summary>
/// <param name="longDisplay"></param>
/// <param name="shortDisplay"></param>
public UnitDisplayAttribute(string longDisplay, string shortDisplay)
{
this.ShortDisplay = shortDisplay;
this.LongDisplay = longDisplay;
}
}这会很好,但是当我将属性应用到上面定义的类时,我会遇到这种情况:
public class MovementSnapshot
{
/// <summary>
/// The total distance traveled, in meters.
/// </summary>
[UnitDisplay("[m]", "Meters")]
public int TotalTravelDistance { get; private set; }
/// <summary>
/// The current speed of the car. In KM/H
/// </summary>
[UnitDisplay("[km/h]", "Kilometers per hour")]
public int Speed { get; private set; }
/// <summary>
/// The maximum speed achieved. In KM/H
/// </summary>
[UnitDisplay("[km/h]", "Kilometers per hour")]
public int MaxSpeed { get; private set; }
}我必须复制以相同单位测量的属性.有些课程有10-12个.然后,我考虑向UnitDisplayAttribute类添加一个构造函数,该类以Distance的一个实例为例,可以提供一个字符串方法来输出显示。
/// <summary>
/// Provides an interface for the display of Unit-type objects
/// </summary>
public class UnitDisplayAttribute
: Attribute
{
/// <summary>
/// The short display of the unit. Example: [N]
/// </summary>
public string ShortDisplay { get; private set; }
/// <summary>
/// Long long display of the unit. Example: Newtons
/// </summary>
public string LongDisplay { get; private set; }
public UnitDisplayAttribute(Distance d)
{
// check if the distance is meters, kilometers etc... and apply the appropriate short and long strings to the property.
}
}这对于上面的MovementSnapshot类很有效,但是将自定义属性应用到上面的Distance枚举会让人感到很尴尬,因为该属性接受相同类型的对象:
/// <summary>
/// Distance measurement units
/// </summary>
public enum Distance
{
/// <summary>
/// Meters.
///</summary>
[UnitDisplay(Distance.M)]
M = 0,
...
}我在这里什么都不担心吗?吉米在看这段代码时会失去理智吗?如何为表示为单元的对象和作为该单元的对象实现良好的显示?
发布于 2015-02-27 02:37:16
我将提出一种完全不同的方法。我喜欢OOP。
创建一个界面。
public interface IUnitOfMeasure
{
int Value { get; set; }
string ShortDescription { get; }
string LongDescription { get; }
}和格式化器Enum
public enum UnitOfMeasureFormat { Short, Long }然后实现了接口、过载和重载的ToString()方法。
public class Meters : IUnitOfMeasure
{
public int Value { get; set; }
public string ShortDescription { get { return "[m]"; }
public string LongDescription { get { return "Meters"; }
public override string ToString()
{
return this.Value + " " + this.ShortDescription;
}
public string ToString(UnitOfMeasureFormat format)
{
if (format == UnitOfMeasureFormat.Short)
{
return this.ToString();
}
return this.Value + " " + this.LongDescription;
}
}现在,您可以将类的属性设置为“更强”的类型。
public class MovementSnapshot
{
public Meters TotalTravelDistance { get; private set; }只需在需要显示ToString()方法时调用它。
myMovementSnapshot.TotalTravelDistance.ToString();我在这里没有这样做,但是此时,您可能想开始考虑实现单元转换方法以及实现IEquatable和IComparable接口。
https://codereview.stackexchange.com/questions/82686
复制相似问题