问题描述
如下图所示,由于第二种原因,我的DirectCast不会有任何原因.它说它需要一个类型,但是,它根本不会带任何对象!
As you seen in this picture below, for some reason my DirectCast wont except ANYTHING for the second argument. It says it requires a type, but, it won't take any object at all!
感谢您的帮助!我正在使用VB.net,因此所有.net答案都可以接受:)
Thanks for any help! I'm using VB.net so all .net answers are acceptable :)
编辑
好,所以显然我没有给它正确的type
类型.有人可以澄清一下吗?假设需要转换为gridElement
的类型,我应该用objType替换什么?
Ok, so apparently I'm not giving it the right kind of type
. Could somebody please clarify this? Assuming the type it needs to cast to is gridElement
, what should I replace objType with?
推荐答案
DirectCast
需要对象原型(即,仅为其提供所需的类名称),而不是System.Type
描述符对象.要使用System.Type
投射对象,您将需要利用 CTypeDynamic ():
DirectCast
requires an object prototype (i.e. just giving it the intended class name) rather than a System.Type
descriptor object. To cast an object using a System.Type
, you will want to utilize CTypeDynamic():
Return CTypeDynamic(createElementByIdAndLayer.MemberwiseClone(), objType)
该错误实际上是在告诉您类型名称为"objType"的类不存在.
The error is essentially telling you a class with the type name "objType" does not exist.
这篇关于未定义'objType'...实际上,为什么会这样呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!