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

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

问题描述

我正在尝试转换 MSDN 文章中提供的示例 Creating动态数据输入用户界面到 C#,但我被困在以下代码中:

I am trying to convert the example provided in MSDN article Creating Dynamic Data Entry User Interfaces to C#, but am stuck at the following code:

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

如何将上述 VB.NET 语句转换为 C#?

How do I convert the above VB.NET statement to C#?

推荐答案

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

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

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