c# - 定义 "custom"整数类型?

标签 c#

我有一个与外部库接口(interface)的程序,除其他外,它有一个打包在更大结构中的无符号 12 位值。

这曾经是 8 位,所以我们简单地将它编码(marshal)为一个字节。

现在它是 12 位...我可以使用 ushort,但这会带来 (a) 范围检查和 (b) 编码(marshal)处理的问题。

是否有一种简单的方法来实现像这样的受限数字类型,我不必重写每个赋值和比较方法?

最佳答案

试试这个(这个例子展示了一个自定义的 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; }
}

在构造函数中实现您喜欢的应用约束。

用法

MyCustomInt64 myInt = 27; //use as like any other value type

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

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);
    }
}

关于c# - 定义 "custom"整数类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7615113/

相关文章:

c# - 设置 Log4Net 以记录类库的输出

c# - 哪个数学函数会给我正确的舍入功能?

c# - EF 4.3.1 在 LinqToEntities 查询中包含继承的导航属性

c# - 从 C# 中的存储过程捕获错误

c# - X 是枚举——这符合规范吗?

c# - Java Runtime Environment 与.NET Framework 在编译过程方面相比如何?

c# - GetHashCode问题

c# - 在根目录中获取文件数组的最有效方法(如果存在)

c# - new { }(不带 [])是什么意思?

c# - 如何检查MediaPlayer是否已完成声音加载?