如何将 VB.Net 的 CType() 转换为 C#

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12249528/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 22:37:15  来源:igfitidea点击:

How do I translate VB.Net's CType() to C#

c#.netvb.netcasting

提问by Michael

I Have this code segment in VB NET:

我有这个代码段VB NET

CType(pbImageHolder.Image, Bitmap).SetPixel(curPoint.X, curPoint.Y, Color.Purple)

what is appropriate code in C#?

什么是合适的代码C#

Thank you in advance.

先感谢您。

采纳答案by Dennis Traub

In VB.Net CType(object, type)casts an object to a specific type.

在 VB.Net 中,CType(object, type)将对象转换为特定类型。

There are two ways to accomplish this in C#:

在 C# 中有两种方法可以实现这一点:

Bitmap image = pbImageHolder.Image as Bitmap;
image.SetPixel(curPoint.X, curPoint.Y, Color.Purple);

or

或者

Bitmap image = (Bitmap)(pbImageHolder.Image);
image.SetPixel(curPoint.X, curPoint.Y, Color.Purple);

回答by Dennis

((Bitmap)pbImageHolder.Image).SetPixel(curPoint.X, curPoint.Y, Color.Purple)

回答by Rajesh Pathakoti

Hi this is the code after conversion VB to C# code:

嗨,这是将 VB 转换为 C# 代码后的代码:

((Bitmap)pbImageHolder.Image).SetPixel(curPoint.X, curPoint.Y, Color.Purple);

And if you want code conversion from VB to C# and vice verse go through the following link: http://www.developerfusion.com/tools/convert/vb-to-csharp/

如果您想将代码从 VB 转换为 C#,反之亦然,请访问以下链接:http: //www.developerfusion.com/tools/convert/vb-to-csharp/