基本上我遇到了这些代码是粗体的错误:
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); } } }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 byteI'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); } } }最满意答案
不幸的是,C#中的泛型转换规则不允许这样直接,但你可以通过object 。 例如:
byte[] data = (byte[]) (object) value;但是,您可能会考虑是否真的适合将它作为泛型方法......它并不像它能够处理所有类型,或者对于它所接受的所有类型都完全相同。
我还强烈建议解压缩表达式((XDevkit.IXboxConsole) this.XboxConsole) ,它用于所有成功案例。
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.
更多推荐
发布评论