Cannot convert type 'T' to bool

Basically I'm having these errors where the code is in bold:

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

I've been looking all over google and cannot seem to find a way around this, thanks in advance

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);
            }
        }
    }
Jon Skeet
people
quotationmark

Unfortunately the rules for generic conversions in C# don't allow for this directly, but you can go via object. For example:

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

However, you might consider whether it's really appropriate for this to be a generic method anyway... it's not like it copes with all types, or does exactly the same thing for all of the types that it does accept.

I'd also strongly recommend extracting the expression ((XDevkit.IXboxConsole) this.XboxConsole) which is used in all the success cases.

people

See more on this question at Stackoverflow