无法将类型'T'转换为布尔值(Cannot convert type 'T' to bool)

编程入门 行业动态 更新时间:2024-10-07 15:21:58
无法将类型'T'转换为布尔值(Cannot convert type 'T' to bool)

基本上我遇到了这些代码是粗体的错误:

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 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); } } }

最满意答案

不幸的是,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.

更多推荐

本文发布于:2023-08-07 18:26:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465267.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   类型   布尔值   bool   type

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!