C#中的等价于VB.Net的DirectCast? [英] C#'s equivalent to VB.Net's DirectCast?

查看:819
本文介绍了C#中的等价于VB.Net的DirectCast?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能已经问过,但如果有,我找不到它。
C#有一个相当于VB.Net的DirectCast?
我知道它有()蒙上和'为'关键字,但那些排队CTYPE和TryCast。



需要明确的是,这些关键字请执行下列操作;



类型/()蒙上:如果它已经是正确的类型,投它,否则寻找一个类型转换器和调用它。如果没有找到类型转换器,抛出一个InvalidCastException



TryCast /作为关键字:如果是正确的类型,投它,否则返回null



DirectCast :如果是正确的类型,投它,否则抛出一个InvalidCastException



修改:我已经阐述了上述后,一些人还回应称,()是等价的,所以我将进一步扩大在为什么这是不正确的。



DirectCast只允许任何变窄或在继承树扩大转换,它不支持的转换在不同的分支状()做即:



C#,这编译和运行:

  //此代码使用类型转换器跨去继承树
双D = 10;
INT I =(INT)D;



VB.Net,这不会编译



<前类=朗动prettyprint-覆盖> 直接投只能上升或下降一个分支,从来没有应用到不同的。
昏暗-d作为双= 10
暗淡我作为整数= DirectCast(D,整型)

在VB.Net到我的C#代码相对应的是CTYPE:



<预类=朗动prettyprint-覆盖> '这编译和运行
昏暗-d作为双= 10
暗淡我作为整数= CTYPE(D,整型)

(,再次编辑我本来使用字符串,我改成了一倍...对不起)


解决方案

似乎很清楚,你想要的功能在C#。试试这个,但..

 静态牛逼DirectCast< T>(对象o,类型型),其中T:类
(!(type.IsInstanceOfType(O))){
如果
{
抛出新的ArgumentException();
}
的T值= O为T;
如果(价值== NULL和放大器;&安培;!O = NULL)
{
抛出新InvalidCastException的();
}
返回值;
}

或者,即使它是从VB调用我喜欢不同的。



 静态牛逼DirectCast< T>(对象o)其中T:类
{
的T值= O为T ;
如果(价值== NULL和放大器;&安培;!O = NULL)
{
抛出新InvalidCastException的();
}
返回值;
}


This has probably been asked before, but if it has, I can't find it. Does C# have an equivalent to VB.Net's DirectCast? I am aware that it has () casts and the 'as' keyword, but those line up to CType and TryCast.

To be clear, these keywords do the following;

CType/() casts: If it is already the correct type, cast it, otherwise look for a type converter and invoke it. If no type converter is found, throw an InvalidCastException.

TryCast/"as" keyword: If it is the correct type, cast it, otherwise return null.

DirectCast: If it is the correct type, cast it, otherwise throw an InvalidCastException.

EDIT: After I have spelled out the above, some people have still responded that () is equivalent, so I will expand further upon why this is not true.

DirectCast only allows for either Narrowing or Widening conversions on inheritance tree, it does not support conversions across different branches like () does ie:

C#, this compiles and runs:

//This code uses a type converter to go across an inheritance tree
double d = 10;
int i = (int)d;

VB.Net, this does NOT COMPILE

'Direct cast can only go up or down a branch, never across to a different one.
Dim d As Double = 10
Dim i As Integer = DirectCast(d, Integer)

The equivalent in VB.Net to my C# code is CType:

'This compiles and runs
Dim d As Double = 10
Dim i As Integer = CType(d, Integer)

(Edit again, I was originally using strings, I changed it to double... sorry)

解决方案

It seems clear that the functionality you want is not in C#. Try this though..

    static T DirectCast<T>(object o, Type type) where T : class
    {
        if (!(type.IsInstanceOfType(o)))
        {
            throw new ArgumentException();
        }
        T value = o as T;
        if (value == null && o != null)
        {
            throw new InvalidCastException();
        }
        return value;
    }

Or, even though it is different from the VB call I like

static T DirectCast<T>(object o) where T : class
{
    T value = o as T;
    if (value == null && o != null)
    {
        throw new InvalidCastException();
    }
    return value;
}

这篇关于C#中的等价于VB.Net的DirectCast?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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