为什么Windows窗体设计器投INT到字节再回到int对于FromArgb [英] Why does the windows forms designer cast int to byte then back to int for FromArgb

查看:87
本文介绍了为什么Windows窗体设计器投INT到字节再回到int对于FromArgb的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找通过今天的一些代码,看到类似以下内容:

I was looking through some code today and saw something like the following:

var colour = Color.FromArgb(((int)(((byte)(227)))), ((int)(((byte)(213)))), ((int)(((byte)(193)))));

当我问为什么它是如此,因为ReSharper的证实,所有的演员都是多余的,有人告诉我,设计者做了这样的说法,他们已经复制了。

When I asked why it was so, since Resharper confirmed that all the casts are redundant, I was told that the designer done it that way and they had copied that.

我一看果然设计器生成一个属性设置为自定义时,同上面的代码颜色。

I had a look and sure enough the designer generates code the same as above when setting a property to a custom colour.

有谁知道为什么设计师会做到这一点?它似乎并没有作出从表面意义上说,除非我失去了一些东西?

Does anyone know why the designer would do this? It doesn't appear to make sense on the face of it, unless I am missing something?

推荐答案

此代码是自动通过内置的的WinForms设计师代码序列化产生的。这里的有罪的一方是System.Drawing.ColorConverter类的TypeConverter的彩色。在它的ConvertTo()方法的相关代码:

This code is auto-generated by the code serializer built into the Winforms designer. The guilty party here is the System.Drawing.ColorConverter class, the TypeConverter for Color. The relevant code in its ConvertTo() method is:

   member = typeof(Color).GetMethod("FromArgb", new Type[] { typeof(int), typeof(int), typeof(int) });
   arguments = new object[] { color2.R, color2.G, color2.B };

的R,G和B属性返回一个字节。因此,代码串首先生成的整数文字和应用(字节)转换为相匹配的参数类型。然后看见了FromArgb()接受整数参数,以便应用(INT)铸。

The R, G and B properties return a byte. So the code serializer first generates the integer literal and applies the (byte) cast to match the arguments type. Then sees that FromArgb() accepts integer arguments so applies the (int) cast.

这仅仅是疯狂的机器生成的代码。它只有是正确的,它不必是相当

This is just maniacal machine generated code. It only has to be correct, it doesn't have to be pretty.

这篇关于为什么Windows窗体设计器投INT到字节再回到int对于FromArgb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆