定义"自定义"基于整数的类型?

Joe*_*Joe 17 c#

我有一个与外部库连接的程序,除了其他外,它还有一个无符号的12位值,包含在一个更大的结构中.

这曾经是8位,所以我们只是将其编组为一个字节.

现在它是12位...我可以使用ushort,但这会打开(a)范围检查和(b)编组的问题.

是否有一种简单的方法来实现这样的约束数值类型,我不必覆盖每个赋值和比较方法?

ker*_*ode 21

试试这个(这个例子显示了一个自定义的Int64类型)

public class MyCustomInt64 : CustomValueType<MyCustomInt64, Int64>
{
    private MyCustomInt64(long value) : base(value) {}        
    public static implicit operator MyCustomInt64(long value) { return new MyCustomInt64(value); }
    public static implicit operator long(MyCustomInt64 custom) { return custom._value; }
}
Run Code Online (Sandbox Code Playgroud)

在构造函数中实现您喜欢的内容以应用constriants.

用法

MyCustomInt64 myInt = 27; //use as like any other value type
Run Code Online (Sandbox Code Playgroud)

这里是可重用的基本自定义值类型(如果需要,添加更多运算符)

public class CustomValueType<TCustom, TValue>
{        
    protected readonly TValue _value;

    public CustomValueType(TValue value)
    {
        _value = value;
    }

    public override string ToString()
    {
        return _value.ToString();
    }

    public static bool operator <(CustomValueType<TCustom, TValue> a, CustomValueType<TCustom, TValue> b)
    {
        return Comparer<TValue>.Default.Compare(a._value, b._value) < 0;
    }

    public static bool operator >(CustomValueType<TCustom, TValue> a, CustomValueType<TCustom, TValue> b)
    {
        return !(a < b);
    }

    public static bool operator <=(CustomValueType<TCustom, TValue> a, CustomValueType<TCustom, TValue> b)
    {
        return (a < b) || (a == b);
    }

    public static bool operator >=(CustomValueType<TCustom, TValue> a, CustomValueType<TCustom, TValue> b)
    {
        return (a > b) || (a == b);
    }

    public static bool operator ==(CustomValueType<TCustom, TValue> a, CustomValueType<TCustom, TValue> b)
    {
        return a.Equals((object)b);
    }

    public static bool operator !=(CustomValueType<TCustom, TValue> a, CustomValueType<TCustom, TValue> b)
    {
        return !(a == b);
    }

    public static TCustom operator +(CustomValueType<TCustom, TValue> a, CustomValueType<TCustom, TValue> b)
    {
        return (dynamic) a._value + b._value;
    }

    public static TCustom operator -(CustomValueType<TCustom, TValue> a, CustomValueType<TCustom, TValue> b)
    {
        return ((dynamic) a._value - b._value);
    }

    protected bool Equals(CustomValueType<TCustom, TValue> other)
    {            
        return EqualityComparer<TValue>.Default.Equals(_value, other._value);
    }

    public override bool Equals(object obj)
    {
        if (ReferenceEquals(null, obj)) return false;
        if (ReferenceEquals(this, obj)) return true;
        if (obj.GetType() != this.GetType()) return false;
        return Equals((CustomValueType<TCustom, TValue>)obj);
    }

    public override int GetHashCode()
    {
        return EqualityComparer<TValue>.Default.GetHashCode(_value);
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 这是太棒了!就像发现了隐藏的宝藏一样,真是好东西! (2认同)
  • 但使所有这些结构化-因为我们想要值类型自然 (2认同)

Jac*_*all 15

您应该创建一个覆盖隐式转换运算符的结构:

struct PackedValue {
    private PackedValue(ushort val) {
         if(val >= (1<<12)) throw new ArgumentOutOfRangeException("val");
         this._value = val;
    }
    private ushort _value;
    public static explicit operator PackedValue(ushort value) {
        return new PackedValue(value);
    }

    public static implicit operator ushort(PackedValue me) {
        return me._value;
    }
}
Run Code Online (Sandbox Code Playgroud)