C# 拳击问题
首先,两个例子:
// This works
int foo = 43;
long lFoo = foo;
// This doesn't
object foo = (int)43;
long? nullFoo = foo as long?; // returns null
long lFoo = (long)foo; // throws InvalidCastException
if (foo.GetType() == typeof(int))
Console.WriteLine("But foo is an int..."); // This gets written out
现在,我猜测为什么第二个不起作用是因为拳击。 该代码背后的目的是实现IComparable
。 我需要某种方法来根据需要将对象强制转换为 long 或 ulong,或者如果两者都不是,则抛出错误。 我不想对每个基本数字类型(byte,int,long,ubyte,...)实施检查,我宁愿只在最大的数字类型中捕获它们并以这种方式处理它。 这里所有聪明人的想法是什么? 我怎样才能拆箱对象,最好避免反射,但我想如果这是唯一的方法......或者我应该不实现IComparable
的非泛型版本?
编辑:
这似乎有效,但似乎是解决问题的可怕黑客。 只有我吗?
long lFoo = long.Parse(foo.ToString());
First, two examples:
// This works
int foo = 43;
long lFoo = foo;
// This doesn't
object foo = (int)43;
long? nullFoo = foo as long?; // returns null
long lFoo = (long)foo; // throws InvalidCastException
if (foo.GetType() == typeof(int))
Console.WriteLine("But foo is an int..."); // This gets written out
Now, my guess as to why the second doesn't work is because of boxing. The purpose behind this code is to implement IComparable
. I need some way to coerce an object into either a long or a ulong as appropriate, or if it's neither, than to throw an error. I don't want to have to implement checks for each basic numeric type (byte, int, long, ubyte, ...) I'd rather just catch them in the largest numeric type and deal with it that way. Thoughts from all the smart people here? How can I unbox the object, preferably avoiding reflection, but I suppose if that's the only way... Or should I just not implement the non-generics version of IComparable
?
Edit:
This seems to work, but seems like a horrible hack around the problem. Is it just me?
long lFoo = long.Parse(foo.ToString());
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您转换为值类型时,您实际上是在强制执行拆箱 IL 操作,这要求您要转换的类型与装箱值完全匹配; 没有可以同时发生的隐式或显式转换。
这通常意味着您需要使用类型代码(或者如果使用类型则使用 if/else)进行切换,或者在您的情况下,检查 null 后跟 Convert.ToInt64(),这应该处理它正确。
When you're casting to a value type you're really forcing an unbox IL operation, which requires that the type you're casting to matches exactly the boxed value; there are no conversions, implicit, or explicit that can happen at the same time.
This usually means that you either need to do a switch using the typecode (or an if/else if using types), or, in your case, go with a check for null followed by Convert.ToInt64(), which should deal with it correctly.
不仅仅是你,但是 tryparse 不会引发异常。
Its not just you, however tryparse does not raise an exception.