.net 从int转换为自定义结构C#
我正在编写一个定制的通用向量类,其中T:struct、IFormattable、IComparable、IConvertible。 我可以通过这个[int index]访问向量的索引值。在一个循环中,我用这段代码来制作Vectorate。一:.net 从int转换为自定义结构C#,.net,generics,struct,casting,iconvertible,.net,Generics,Struct,Casting,Iconvertible,我正在编写一个定制的通用向量类,其中T:struct、IFormattable、IComparable、IConvertible。 我可以通过这个[int index]访问向量的索引值。在一个循环中,我用这段代码来制作Vectorate。一: r[i] = (T)Convert.ChangeType(1, typeof(T)); 它适用于int、decimal等标准数字,但当我编写自定义结构uHalfByte进行测试时,它给出了一个错误: 从“System.Int32”到“uHalfByte”
r[i] = (T)Convert.ChangeType(1, typeof(T));
它适用于int、decimal等标准数字,但当我编写自定义结构uHalfByte进行测试时,它给出了一个错误:
从“System.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);
}
}
struct uHalfByte:IFormattable、IComparable、IConvertible
{
私有字节val;
公共字节值
{
获取{return(byte)(val&0xF);}
集合{val=(字节)(值&0xF);}
}
公共字节(字节val)
{
this.val=(字节)(val&0xF);
}
公共字符串到字符串(字符串格式,IFormatProvider formatProvider)
{
如果(formatProvider==null)formatProvider=System.Globalization.CultureInfo.CurrentCulture;
if(string.IsNullOrEmpty(format))format=“G”;
字符串s=“”;
对于(inti=0;i<4;i++)s++=((Val>>i)和1);
返回s;
}
公共整数比较(uHalfByte其他)
{
返回此.Val-其他.Val;
}
公共类型代码GetTypeCode()
{
返回TypeCode.Byte;
}
public bool ToBoolean(IFormatProvider提供程序)
{
返回值!=0;
}
公共字符ToChar(IFormatProvider提供程序)
{
返回(char)val;
}
公共sbyte ToSByte(IFormatProvider提供程序)
{
返回(sbyte)val;
}
公共字节ToByte(IFormatProvider提供程序)
{
返回(字节)val;
}
公共短消息16(IFormatProvider)
{
返回(短)val;
}
公共ushort ToUInt16(IFormatProvider)
{
返回(ushort)val;
}
public int to int32(IFormatProvider提供程序)
{
返回(int)val;
}
公共uint ToUInt32(IFormatProvider)
{
返回(uint)val;
}
公共long-ToInt64(IFormatProvider提供程序)
{
返回(长)val;
}
公共ulong ToUInt64(IFormatProvider)
{
返回(ulong)val;
}
公共浮动到单个(IFormatProvider)
{
返回(浮动)值;
}
公共双ToDouble(IFormatProvider提供程序)
{
返回(双)val;
}
公共十进制到十进制(IFormatProvider提供程序)
{
返回(十进制)val;
}
公共日期时间到日期时间(IFormatProvider)
{
返回新的日期时间(val);
}
公共字符串到字符串(IFormatProvider提供程序)
{
返回到字符串(“提供者”);
}
公共对象到类型(类型转换类型,IFormatProvider提供程序)
{
返回Convert.ChangeType(val,conversionType);
}
公共静态显式运算符uHalfByte(int-val)
{
返回新的uHalfByte((字节)val);
}
}
我在uHalfByte中做错了什么,还是根本不可能?它不会以那种方式工作,因为System.Int32(如异常消息中所述)没有关于自定义类的信息uHalfByte,无法执行该转换。 当然,它将以另一种方式工作uHalfByte->int 编辑:
在您的情况下,我认为您可以使用转换运算符: 像这样:
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));
基于@Thowk答案的解决方案: 实现了一个通用接口:
public interface INumber<T>
{
T ConvertGeneric<T1>(T1 item);
}
成员中的公共接口
{
T(T1项);
}
在我的脚本中添加了接口的实现:
public struct uHalfByte : IFormattable, IComparable<uHalfByte>, IConvertible, INumber<uHalfByte>
{
...
public uHalfByte ConvertGeneric<T>(T item)
{
if (typeof(T) == typeof(int))
{
return new uHalfByte((byte)(int)Convert.ChangeType(item, typeof(int)));
}
else if (typeof(T) == typeof(uint))
{
return new uHalfByte((byte)(uint)Convert.ChangeType(item, typeof(uint)));
}
else if (typeof(T) == typeof(long))
{
return new uHalfByte((byte)(long)Convert.ChangeType(item, typeof(long)));
}
else if (typeof(T) == typeof(ulong))
{
return new uHalfByte((byte)(ulong)Convert.ChangeType(item, typeof(ulong)));
}
else if (typeof(T) == typeof(short))
{
return new uHalfByte((byte)(short)Convert.ChangeType(item, typeof(short)));
}
else if (typeof(T) == typeof(ushort))
{
return new uHalfByte((byte)(ushort)Convert.ChangeType(item, typeof(ushort)));
}
else if (typeof(T) == typeof(byte))
{
return new uHalfByte((byte)Convert.ChangeType(item, typeof(byte)));
}
else if (typeof(T) == typeof(sbyte))
{
return new uHalfByte((byte)(sbyte)Convert.ChangeType(item, typeof(sbyte)));
}
else throw new NotSupportedException(string.Format("Type {0} is not supported, you have to write your own function!", typeof(T)));
}
}
公共结构uHalfByte:IFormattable、IComparable、IConvertible、INumber
{
...
公共uHalfByte转换器通用(T项)
{
if(typeof(T)=typeof(int))
{
返回新的uHalfByte((byte)(int)Convert.ChangeType(item,typeof(int));
}
否则如果(类型(T)=类型(uint))
{
返回新的uHalfByte((byte)(uint)Convert.ChangeType(item,typeof(uint));
}
如果(类型(T)=类型(长))
{
返回新的uHalfByte((byte)(long)Convert.ChangeType(item,typeof(long));
}
否则如果(类型(T)=类型(ulong))
{
返回新的uHalfByte((byte)(ulong)Convert.ChangeType(item,typeof(ulong));
}
否则如果(类型(T)=类型(短))
{
返回新的uHalfByte((byte)(short)Convert.ChangeType(item,typeof(short));
}
否则如果(typeof(T)=typeof(ushort))
{
返回新的uHalfByte((byte)(ushort)Convert.ChangeType(item,typeof(ushort));
}
else if(typeof(T)=typeof(byte))
{
返回新的uHalfByte((byte)Convert.ChangeType(item,typeof(byte));
}
else if(typeof(T)=typeof(sbyte))
{
返回新的uHalfByte((byte)(sbyte)Convert.ChangeType(item,typeof(sbyte));
}
否则抛出新的NotSupportedException(string.Format(“不支持类型{0},您必须编写自己的函数!”,typeof(T));
}
}
但是,没有在结构接口中实现构造函数的选项,也没有使用抽象类作为结构基础的选项。IMyConvertibleExample应该是什么样子?