vb.net VB.NET中带有动态第二个参数(类型)的CType

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15252266/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:38:35  来源:igfitidea点击:

CType in VB.NET with dynamic second parameter (type)

.netvb.netcasting

提问by Brij

In VB.NET CType can be used to convert one type to another.

在 VB.NET 中,CType 可用于将一种类型转换为另一种类型。

CType(expression,type)

I have the "expression" stored in an instance object class, say "objExp". I have the "type" stored in an instance of Type class, say"objType".

我将“表达式”存储在实例对象类中,例如"objExp". 我将“类型”存储在 Type 类的实例中,例如"objType".

I am trying CType(objExp, objType)I am getting compile error, how should I go about it ? "objType" is fetched and assigned at runtime.

我正在尝试CType(objExp, objType)我收到编译错误,我该怎么办?" objType" 在运行时获取和分配。

回答by ESB Developer

Have you looked into the method CTypeDynamic()? The second parameter (ie: The Type) can be set dynamically. Great for when you are using Reflection.

你研究过CTypeDynamic()方法吗?第二个参数(即:Type)可以动态设置。非常适合使用反射。

For example:

例如:

CTypeDynamic(strValueToConvert, objTypeToConvertTo)

回答by JaredPar

This is simply not possible. The CTypeexpression must be passed a Typeinstance which is known at compile time like Integer, String, etc ... It does not accept values which are of type Type

这根本不可能。该CType表达式必须通过的Type这是在像编译时已知实例IntegerString等等......它不接受这是类型的值Type