【发布时间】:2012-09-03 14:12:33
【问题描述】:
我在 VB.NET 中有这个代码段:
CType(pbImageHolder.Image, Bitmap).SetPixel(curPoint.X, curPoint.Y, Color.Purple)
什么是合适的 C# 代码?
【问题讨论】:
我在 VB.NET 中有这个代码段:
CType(pbImageHolder.Image, Bitmap).SetPixel(curPoint.X, curPoint.Y, Color.Purple)
什么是合适的 C# 代码?
【问题讨论】:
在 VB.Net 中,CType(object, type) 将对象转换为特定类型。
在 C# 中有两种方法可以做到这一点:
Bitmap image = pbImageHolder.Image as Bitmap;
image.SetPixel(curPoint.X, curPoint.Y, Color.Purple);
或
Bitmap image = (Bitmap)(pbImageHolder.Image);
image.SetPixel(curPoint.X, curPoint.Y, Color.Purple);
【讨论】:
as 不适用于没有对结果进行空值检查的用例,并且不能定位为 VB CType 运算符的直接等效项; 2)第二个选项有不必要的括号,请参阅下面对您的评论的回答。所以,被否决了,特别是对于as 的疏忽使用。
as。这可能会导致NullReferenceException 提供非信息性。
CType 不投射对象,而是转换它。 DirectCast 或 TryCast 是铸造操作。例如,Dim i As Integer = CType("1", Integer) 有效,但 Dim i As Integer = DirectCast("1", Integer) 无效
((Bitmap)pbImageHolder.Image).SetPixel(curPoint.X, curPoint.Y, Color.Purple)
【讨论】:
((Bitmap)pbImageHolder.Image) 您尝试将 pbImageHolder 转换为位图,这可能行不通。以下将投射图像:(Bitmap)(pbImageHolder.Image)
(TypeWichCastTo)variable.Member 这样的表达式将以这种方式计算:1) 首先访问 Member 的 variable; 2) 然后将Member 转换为TypeWichCastTo。
您好,这是将VB转换为C#代码后的代码:
((Bitmap)pbImageHolder.Image).SetPixel(curPoint.X, curPoint.Y, Color.Purple);
如果你想从 VB 到 C# 的代码转换,反之亦然,请通过以下链接:http://www.developerfusion.com/tools/convert/vb-to-csharp/
【讨论】:
我很惊讶关于这个主题的答案都不正确,所以我决定在这里发帖。您可以通过反编译 VB.NET 程序来验证发生了什么。答案取决于要转换为的类型。
对于引用类型:
Dim value = CType(x, ReferenceType)
var value = (ReferenceType)x;
对于结构:
Dim value = CType(x, StructType)
var value = (x != null) ? ((StructType)x) : default(StructType);
对于预定义的演员表:
Dim value = CDbl(x)
var value = Microsoft.VisualBasic.CompilerServices.Conversions.ToDouble(x)
看起来像这样:
internal static double ToDouble(object Value, NumberFormatInfo NumberFormat)
{
if (Value == null)
{
return 0.0;
}
IConvertible convertible = Value as IConvertible;
if (convertible != null)
{
switch (convertible.GetTypeCode())
{
case TypeCode.Boolean:
if (Value is bool)
{
return 0 - (((bool)Value) ? 1 : 0);
}
return 0 - (convertible.ToBoolean(null) ? 1 : 0);
case TypeCode.SByte:
if (Value is sbyte)
{
return (sbyte)Value;
}
return convertible.ToSByte(null);
case TypeCode.Byte:
if (Value is byte)
{
return (int)(byte)Value;
}
return (int)convertible.ToByte(null);
case TypeCode.Int16:
if (Value is short)
{
return (short)Value;
}
return convertible.ToInt16(null);
case TypeCode.UInt16:
if (Value is ushort)
{
return (int)(ushort)Value;
}
return (int)convertible.ToUInt16(null);
case TypeCode.Int32:
if (Value is int)
{
return (int)Value;
}
return convertible.ToInt32(null);
case TypeCode.UInt32:
if (!(Value is uint))
{
return convertible.ToUInt32(null);
}
return (uint)Value;
case TypeCode.Int64:
if (Value is long)
{
return (long)Value;
}
return convertible.ToInt64(null);
case TypeCode.UInt64:
if (!(Value is ulong))
{
return convertible.ToUInt64(null);
}
return (ulong)Value;
case TypeCode.Decimal:
if (Value is decimal)
{
return convertible.ToDouble(null);
}
return Convert.ToDouble(convertible.ToDecimal(null));
case TypeCode.Single:
if (Value is float)
{
return (float)Value;
}
return convertible.ToSingle(null);
case TypeCode.Double:
if (Value is double)
{
return (double)Value;
}
return convertible.ToDouble(null);
case TypeCode.String:
return ToDouble(convertible.ToString(null), NumberFormat);
}
}
throw new InvalidCastException(Utils.GetResourceString("InvalidCast_FromTo", Utils.VBFriendlyName(Value), "Double"));
}
【讨论】: