从int转换为自定义结构C#

时间:2017-10-17 12:41:58

标签: .net generics struct casting iconvertible

我正在编写一个自定义通用VectorN类,其中T:struct,IFormattable,IComparable,IConvertible。 我可以通过这个[int index]访问vector的索引值。所以在循环中我做这段代码来制作VectorN.One:

r[i] = (T)Convert.ChangeType(1, typeof(T));

它与标准数字(如int,decimal等)完美配合,但是当我编写自定义结构uHalfByte进行测试时,它会出错:

  

来自&Systems.Int32'的无效广播到' uHalfByte'。

这是uHalfByte的脚本:

struct uHalfByte : IFormattable, IComparable<uHalfByte>, IConvertible
{
    private byte val;
    public byte Val
    {
        get { return (byte)(val & 0xF); }
        set { val = (byte)(value & 0xF); }
    }
    public uHalfByte(byte val)
    {
        this.val = (byte)(val & 0xF);
    }

    public string ToString(string format, IFormatProvider formatProvider)
    {
        if (formatProvider == null) formatProvider = System.Globalization.CultureInfo.CurrentCulture;
        if (string.IsNullOrEmpty(format)) format = "G";
        string s = "";
        for (int i = 0; i < 4; i++) s += ((Val >> i) & 1).ToString(format,formatProvider);
        return s;
    }

    public int CompareTo(uHalfByte other)
    {
        return this.Val - other.Val;
    }

    public TypeCode GetTypeCode()
    {
        return TypeCode.Byte;
    }

    public bool ToBoolean(IFormatProvider provider)
    {
        return val!=0;
    }

    public char ToChar(IFormatProvider provider)
    {
        return (char)val;
    }

    public sbyte ToSByte(IFormatProvider provider)
    {
        return (sbyte)val;
    }

    public byte ToByte(IFormatProvider provider)
    {
        return (byte)val;
    }

    public short ToInt16(IFormatProvider provider)
    {
        return (short)val;
    }

    public ushort ToUInt16(IFormatProvider provider)
    {
        return (ushort)val;
    }

    public int ToInt32(IFormatProvider provider)
    {
        return (int)val;
    }

    public uint ToUInt32(IFormatProvider provider)
    {
        return (uint)val;
    }

    public long ToInt64(IFormatProvider provider)
    {
        return (long)val;
    }

    public ulong ToUInt64(IFormatProvider provider)
    {
        return (ulong)val;
    }

    public float ToSingle(IFormatProvider provider)
    {
        return (float)val;
    }

    public double ToDouble(IFormatProvider provider)
    {
        return (double)val;
    }

    public decimal ToDecimal(IFormatProvider provider)
    {
        return (decimal)val;
    }

    public DateTime ToDateTime(IFormatProvider provider)
    {
        return new DateTime(val);
    }

    public string ToString(IFormatProvider provider)
    {
        return ToString("", provider);
    }

    public object ToType(Type conversionType, IFormatProvider provider)
    {
        return Convert.ChangeType(val, conversionType);
    }

    public static explicit operator uHalfByte(int val)
    {
        return new uHalfByte((byte)val);
    }
}

我是否在uHalfByte中做错了什么或者这是不可能的?

2 个答案:

答案 0 :(得分:1)

它不会以这种方式工作,因为 System.Int32 (如异常消息中所述)没有关于您的自定义类 uHalfByte 的信息,并且无法执行转换。 当然它会以其他方式工作 uHalfByte - &gt; int

编辑:
在你的情况下,我认为你可以使用转换运算符: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/using-conversion-operators

像这样:

struct uHalfByte
{
    public static explicit operator uHalfByte(int value)
    {
        return new uHalfByte();
    }
    public static explicit operator uHalfByte(string value)
    {
        return new uHalfByte();
    }
}

用法:

if (typeof(T) == typeof(uHalfByte)) 
    r[i] = (uHalfByte)value;
else
    r[i] = (T)Convert.ChangeType(value, typeof(T));

答案 1 :(得分:0)

基于@Thowk答案的解决方案: 实现了通用接口:

find . \( -regex ".*ABC.*" -and ! -regex ".*DEF.*" \)  -exec cp {} /destination/folder/ \;

在我的脚本中添加了界面的实现:

public interface INumber<T>
{
    T ConvertGeneric<T1>(T1 item);
}