C# 无法转换类型';T';哄骗

C# 无法转换类型';T';哄骗,c#,C#,基本上,在代码以粗体显示的情况下,我会遇到以下错误: Cannot convert type 'T' to bool Cannot convert type 'T' to string x2 Cannot convert type 'T' to byte[] Cannot convert type 'T' to byte 我一直在谷歌搜索,似乎找不到解决这个问题的方法,提前谢谢 public void Append<T>(T value, bool littleEndian = f

基本上,在代码以粗体显示的情况下,我会遇到以下错误:

Cannot convert type 'T' to bool
Cannot convert type 'T' to string x2
Cannot convert type 'T' to byte[]
Cannot convert type 'T' to byte
我一直在谷歌搜索,似乎找不到解决这个问题的方法,提前谢谢

public void Append<T>(T value, bool littleEndian = false)
    {
        try
        {
            System.Type t = typeof(T);
            if (!this.IsValidType(t))
            {
                throw new Exception("msg_t.AppendMessage: Invalid type!");
            }
            if (t == typeof(bool))
            {
                ((XDevkit.IXboxConsole) this.XboxConsole).WriteInt32(this.DataBuffer + this.MessageLength, ***((bool) value)*** ? 1 : 0);
                this.MessageLength += 4;
            }
            else if (t == typeof(string))
            {
                ((XDevkit.IXboxConsole) this.XboxConsole).WriteString(this.DataBuffer + this.MessageLength, ***(string) value)***;
                this.MessageLength += (uint) Encoding.UTF8.GetBytes(***(string) value)***.Length;
            }
            else if (t == typeof(byte[]))
            {
                byte[] data = ***(byte[]) value***;
                ((XDevkit.IXboxConsole) this.XboxConsole).SetMemory(this.DataBuffer + this.MessageLength, data);
                this.MessageLength += (uint) data.Length;
            }
            else if (t == typeof(byte))
            {
                ((XDevkit.IXboxConsole) this.XboxConsole).WriteByte(this.DataBuffer + this.MessageLength, ***(byte) value***);
                this.MessageLength++;
            }
            else
            {
                byte[] array = (byte[]) typeof(BitConverter).GetMethod("GetBytes", new System.Type[] { value.GetType() }).Invoke(null, new object[] { value });
                if (!littleEndian)
                {
                    Array.Reverse(array);
                }
                ((XDevkit.IXboxConsole) this.XboxConsole).SetMemory(this.DataBuffer + this.MessageLength, array);
                this.MessageLength += (uint) array.Length;
            }
            this.UpdateOverflowedBoolean();
        }
        catch
        {
            if (MessageBox.Show("Error when writing stats!", "GHOSTS", MessageBoxButtons.RetryCancel, MessageBoxIcon.Hand) == DialogResult.Retry)
            {
                this.Append<T>(value, littleEndian);
            }
        }
    }
public void Append(T值,bool littleEndian=false)
{
尝试
{
系统类型t=类型(t);
如果(!this.IsValidType(t))
{
抛出新异常(“msg_t.AppendMessage:无效类型!”);
}
if(t==typeof(bool))
{
((XDevkit.IXboxConsole)this.XboxConsole).WriteInt32(this.DataBuffer+this.MessageLength,***((bool)值)***?1:0);
此.MessageLength+=4;
}
else if(t==typeof(string))
{
((XDevkit.IXboxConsole)this.XboxConsole).WriteString(this.DataBuffer+this.MessageLength,***(字符串)值)***;
this.MessageLength+=(uint)Encoding.UTF8.GetBytes(***(字符串)值)***.Length;
}
else if(t==typeof(byte[]))
{
字节[]数据=***(字节[])值***;
((XDevkit.IXboxConsole)this.XboxConsole).SetMemory(this.DataBuffer+this.MessageLength,data);
this.MessageLength+=(uint)data.Length;
}
else if(t==typeof(byte))
{
((XDevkit.IXboxConsole)this.XboxConsole).WriteByte(this.DataBuffer+this.MessageLength,***(byte)值***);
这个.MessageLength++;
}
其他的
{
byte[]数组=(byte[])typeof(BitConverter).GetMethod(“GetBytes”,新系统.Type[]{value.GetType()}).Invoke(null,新对象[]{value});
如果(!littleEndian)
{
数组。反向(数组);
}
((XDevkit.IXboxConsole)this.XboxConsole).SetMemory(this.DataBuffer+this.MessageLength,数组);
this.MessageLength+=(uint)array.Length;
}
this.UpdateOverflowedBoolean();
}
抓住
{
if(MessageBox.Show(“写入统计信息时出错!”,“重影”,MessageBoxButtons.RetryCancel,MessageBoxIcon.Hand)=DialogResult.Retry)
{
这个.Append(value,littleEndian);
}
}
}

不幸的是,C中的泛型转换规则不允许直接进行转换,但您可以通过
对象
。例如:

byte[] data = (byte[]) (object) value;

但是,不管怎样,你可以考虑这是否是一个通用的方法。它并不是处理所有类型,或者对它所接受的所有类型做完全相同的事情


我还强烈建议提取表达式
((XDevkit.IXboxConsole)this.XboxConsole)
,该表达式在所有成功案例中都会用到。

该方法在我看来不像是通用的。您对这种强制转换有更深入的解释吗?为什么每次都需要使用(object),泛型类型不应该总是一个对象吗?@Sam:不-例如,它可能是一个值类型。当我有时间时,我会尝试添加更多细节。我尝试使用枚举和结构(也是本机的),但每次它都会成功地将参数转换为具有(object)的对象。@Sam:是的,因为这样做。关键是没有从
t
转换到某个任意类型,因为
t
可能是一个值类型。如果将此代码放入LinqPad,则会出现运行时错误,例如
InvalidCastException:无法将“System.Int32”类型的对象强制转换为“System.Byte[]”类型。