什么是C#等价的CType在VB.NET? [英] What is the C# equivalent of CType in VB.NET?

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

问题描述

我正在尝试转换此MSDN文章( http:/ /msdn.microsoft.com/en-us/library/aa479330.aspx )到C#,但被困在以下代码:

I am trying to convert the example provided in this MSDN article (http://msdn.microsoft.com/en-us/library/aa479330.aspx) to C#, but am stuck at the following code:

CType(dq, IUIBuildingBlock).QuestionText = reader("QuestionText")

推荐答案

在C#中,可以有任何C#guru在上面的VB.NET语句转换为C#您可以通过将要转换的类型放在括号中要转换的引用变量前面((type)instance )来指定转换。

In C#, you can specify a cast by putting the type you want to cast to in parenthesis in front of the reference variable that you want to cast ((type)instance).

因此,要将对象( dq )转换为类型 IUIBuildingBlock ,您可以使用以下代码:

So, to cast the object (dq) to the type IUIBuildingBlock, you could use the following code:

((IUIBuildingBlock)dq).QuestionText = reader("QuestionText");

(注意,如果转换是在一个没有实现的对象上完成的, IUIBuildingBlock ,但是 CType ,所以我认为这不是问题。)

(Note that this will throw an exception if the cast is done on an object that doesn't implement IUIBuildingBlock, but so will CType, so I assume that is not a problem.)

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

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