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

10

我有以下VB.NET代码段:

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

C#中什么样的代码是合适的?

4个回答

26

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

5
你提供的第一个选项强烈要求进行空值检查;as并不适用于没有结果空值检查的情况,并且不能被视为VB CType运算符的直接等效物;第二个选项有不必要的括号,参见下面评论的答案。因此,我投了反对票,特别是对于as的不良使用。 - Dennis
正如Dennis所说,在这种情况下不要使用as,因为它可能会导致一个无信息的NullReferenceException异常。 - Jeppe Stig Nielsen
1
@Dennis 给出了很好的建议,但你不需要仅因此而投反对票。这个答案仍然是有用的。 - SysDragon
CType 不是强制转换对象,而是将其转换。 DirectCastTryCast 是强制转换操作。例如, Dim i As Integer = CType("1",Integer) 是有效的,但 Dim i As Integer = DirectCast("1", Integer) 不是。 - SSS

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

使用 ((Bitmap)pbImageHolder.Image) 试图将 pbImageHolder 强制转换为 Bitmap,这可能不起作用。以下代码将强制转换图像:(Bitmap)(pbImageHolder.Image) - Dennis Traub

2

-1

我很惊讶在这个主题上没有一个正确的答案,所以我决定在这里发布。您可以通过反编译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"));
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接