在运行时分配类型 [英] Assign Type at Runtime

查看:68
本文介绍了在运行时分配类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着仔细复制我在这个组中找到的一些代码,因为

在运行时分配了一个类型,但我无法让它工作。可以有人

看看我的逻辑有什么问题吗?


谢谢,


Private Sub Button1_Click(ByVal发送者)作为System.Object,ByVal e

As System.EventArgs)处理Button1.Click

Dim s As String

s =" blah"

Dim t As Type

t = s.GetType

Dim obj As Object

obj = System.Activator.CreateInstance (t)''缺少异常

未处理 - 没有为此对象定义无参数构造函数。

obj =" hello"

s = DirectCast( obj,String)

RichTextBox1.Text = s

End Sub

解决方案

< blockquote> blisspikle写道:


我试图密切复制我在这个组中找到的一些代码,因为

在运行时指定了一个类型,但是我无法让它发挥作用。可以有人

看看我的逻辑有什么问题吗?



< snip>


Private Sub Button1_Click(ByVal sender As System.Object,ByVal e

As System.EventArgs)处理Button1.Click

Dim s As String

s =" blah"

Dim t As Type

t = s.GetType

Dim obj As Object

obj = System.Activator.CreateInstance(t)''缺少异常

未处理 - 没有为此对象定义无参数构造函数。

obj =" hello"

s = DirectCast(obj,String)

RichTextBox1.Text = s

End Sub



你得到的例外是因为,实际上,String中没有无参数的

构造函数(试试自己,输入 ; Dim S As New

字符串(......你会看到)。


另外,当你指定hello时obj,一个新的字符串对象是creat ed

,它最终是分配给s的*对象。我想你想要的是什么




< aircode>

Dim S As String =" Blah"

Dim T As Type = S.GetType

Dim O As Object = _

System.Activator.CreateInstance(T," hello" .ToCharArray )

S = DirectCast(O,String)

< / aircode>


但我仍然没有得到重点是,除非你想实现

以外的其他东西...


HTH。


此致,


Branco。


1月31日上午10点16分,blisspikle < eklas ... @ metforming.comwrote:


我试图密切复制我在这个组中找到的一些代码

分配一个在运行时键入,但我不能让它工作。可以有人

看看我的逻辑有什么问题吗?


谢谢,


Private Sub Button1_Click(ByVal发送者)作为System.Object,ByVal e

As System.EventArgs)处理Button1.Click

Dim s As String

s =" blah"

Dim t As Type

t = s.GetType

Dim obj As Object

obj = System.Activator.CreateInstance (t)''缺少异常

未处理 - 没有为此对象定义无参数构造函数。

obj =" hello"

s = DirectCast( obj,String)

RichTextBox1.Text = s

End Sub



问题与消息完全一样表示,字符串类型有

没有公共无参数构造函数。你需要使用一个

可能的构造函数,例如带有字符数组的构造函数:


obj = System.Activator.CreateInstance(t,New对象()

{" hello" .ToCharArray()})


1月31日上午11点16分, " blisspikle" < eklas ... @ metforming.comwrote:


我试图密切复制我在这个组中找到的一些代码

分配一个在运行时键入,但我不能让它工作。可以有人

看看我的逻辑有什么问题吗?


谢谢,


Private Sub Button1_Click(ByVal发送者)作为System.Object,ByVal e

As System.EventArgs)处理Button1.Click

Dim s As String

s =" blah"

Dim t As Type

t = s.GetType

Dim obj As Object

obj = System.Activator.CreateInstance (t)''缺少异常

未处理 - 没有为此对象定义无参数构造函数。



因为System.String没有无参数构造函数...在其他

字中,你不能调用新的String()一个字符串。你需要传递

这样的东西:

obj = System.Activateor.CreateInstance(t,New Object [] {" Hello"})


s = DirectCast(obj,String)

RichTextBox1.Text = s

End Sub



HTH,

-

Tom Shelton


I tried closely copying some code that I found on this group for
assigning a type at runtime, but I cannot get it to work. Can someone
see what is wrong with my logic?

Thanks,

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Dim s As String
s = "blah"
Dim t As Type
t = s.GetType
Dim obj As Object
obj = System.Activator.CreateInstance(t) ''Missing Exception
Unhandled - No parameterless constructor defined for this object.
obj = "hello"
s = DirectCast(obj, String)
RichTextBox1.Text = s
End Sub

解决方案

blisspikle wrote:

I tried closely copying some code that I found on this group for
assigning a type at runtime, but I cannot get it to work. Can someone
see what is wrong with my logic?

<snip>

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Dim s As String
s = "blah"
Dim t As Type
t = s.GetType
Dim obj As Object
obj = System.Activator.CreateInstance(t) ''Missing Exception
Unhandled - No parameterless constructor defined for this object.
obj = "hello"
s = DirectCast(obj, String)
RichTextBox1.Text = s
End Sub

You''re getting the exception because, really, there''s no parameterless
constructor in String (try for yourself, type "Dim S As New
String(" ... and you''ll see).

Also, when you assign "hello" to obj, a new string object is created
and it''s *that* object that is assigned to s, in the end. I guess what
you want is:

<aircode>
Dim S As String = "Blah"
Dim T As Type = S.GetType
Dim O As Object = _
System.Activator.CreateInstance(T, "hello".ToCharArray)
S = DirectCast(O, String)
</aircode>

But I still don''t get the point, unless you want to instanciate
something other than a string...

HTH.

Regards,

Branco.


On Jan 31, 10:16 am, "blisspikle" <eklas...@metforming.comwrote:

I tried closely copying some code that I found on this group for
assigning a type at runtime, but I cannot get it to work. Can someone
see what is wrong with my logic?

Thanks,

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Dim s As String
s = "blah"
Dim t As Type
t = s.GetType
Dim obj As Object
obj = System.Activator.CreateInstance(t) ''Missing Exception
Unhandled - No parameterless constructor defined for this object.
obj = "hello"
s = DirectCast(obj, String)
RichTextBox1.Text = s
End Sub

The problem is exactly as the message indicates, the string type has
no public parameterless constructor. You need to use one of the
possible constructors, for example the one taking a character array:

obj = System.Activator.CreateInstance(t, New Object()
{"hello".ToCharArray()})


On Jan 31, 11:16 am, "blisspikle" <eklas...@metforming.comwrote:

I tried closely copying some code that I found on this group for
assigning a type at runtime, but I cannot get it to work. Can someone
see what is wrong with my logic?

Thanks,

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Dim s As String
s = "blah"
Dim t As Type
t = s.GetType
Dim obj As Object
obj = System.Activator.CreateInstance(t) ''Missing Exception
Unhandled - No parameterless constructor defined for this object.

Because System.String has no parameterless constructor... In other
words, you can''t call new String() on a string. You need to pass in
something like this:
obj = System.Activateor.CreateInstance (t, New Object[] {"Hello"})

s = DirectCast(obj, String)
RichTextBox1.Text = s
End Sub

HTH,

--
Tom Shelton


这篇关于在运行时分配类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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