在运行时为变量分配'type' [英] Assigning a 'type' to a variable at runtime

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

问题描述

我正在尝试找到一种在运行时创建给定类型变量的方法

我不知道该类型直到它实际执行。例如,


dim x as object =" hi"


x被声明为对象,但x.gettype返回''string' ',所以它知道它

包含一个字符串。


如果我想创建一个变量y与变量x包含的相同类型

如果不必通过每个可能的

类型进行迭代,我将如何做到这一点?也就是说,对于上面的例子,我希望相当于dim y

as string因为x.gettype是字符串。另一个例子:


dim x as object = new MyClass


x.gettype会返回''MyClass''所以我想要等价物dim y

作为myclass。


尽管这样做是不合法的Dim y as x.gettype.name那就是我想要做的事情。


无论如何要实现这个目标吗?


谢谢, John

解决方案

>虽然这样做是不合法的Dim y as x.gettype.name"那就是

我到底想要做什么。

无论如何要做到这一点吗?




No.为什么你认为你需要这个?您将如何处理

变量?为什么你不能简单地使用Object作为第二个变量

那么好?

Mattias


-

Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com

请回复到新闻组。


JohnR,


我们在这个新闻组中看到的更多,而且我认为永远,将会是什么

使用此解决方案。听起来像是在一个容器中扔苹果和梨,而不是再把它们分开。为什么不直接在两个

容器中。这可能会让苹果和梨的味道更好。


你能解释一下这个问题背后的理由是什么吗? />

(那个容器没有告诉它是什么,是'对象'')。


Cor

" JohnR" <乔****** @ hotmail.com> schreef in bericht

新闻:RqH2f.9102


tU1.1605@trndny07 ...

我正在努力寻找一种在运行时创建给定类型的变量的方法
在实际执行之前我不知道类型。例如,

dim x as object =" hi"

x被声明为对象,但x.gettype返回''string'',因此它知道它
包含一个字符串。

如果我想创建一个变量y与变量x
包含的相同类型包含如何进行,而不必通过每种可能的类型进行迭代?也就是说,对于上面的例子,我希望相当于dim
y as string。因为x.gettype是字符串。另一个例子:

dim x as object = new MyClass

x.gettype将返回''MyClass''所以我想要相当于dim y
作为myclass"。

虽然这样做是不合法的Dim y as x.gettype.name那就是我想要做的事情。

无论如何都要做到这一点吗?

谢谢,约翰



I''m trying to find a way to create a variable of a given type at runtime
where I won''t know the type until it actually executes. For example,

dim x as object = "hi"

x is declared as an object but x.gettype returns ''string'', so it knows it
contains a string.

If I want to create a variable "y" as the same type that variable x contains
how would I do that without having to iterate thru every possible
type? That is, for the above example I would want the equivalent of "dim y
as string" because x.gettype is string. Another example:

dim x as object = new MyClass

x.gettype would return ''MyClass'' so I would want the equivalent of "dim y
as myclass".

Although it''s not legal to do this "Dim y as x.gettype.name" that is
exactly what I want to do.

Is there anyway to accomplish this?

Thanks, John

解决方案

>Although it''s not legal to do this "Dim y as x.gettype.name" that is

exactly what I want to do.

Is there anyway to accomplish this?



No. Why do you think you need this? What will you do with the
variable? Why can''t you simply use Object for the second variable as
well?
Mattias

--
Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


JohnR,

We see this more in this newsgroup and than I think forever, what will be
the use of this solution. It sounds for me as throwing apples and pears in
one container and than split them again afterwards. Why not direct in two
containers. That will probably keep the taste of the apples and the pears
much better.

Can you explain to us what is your reason behind this question?

(That container withouth telling what it is, is of couse ''object'').

Cor
"JohnR" <Jo******@hotmail.com> schreef in bericht
news:RqH2f.9102


tU1.1605@trndny07...

I''m trying to find a way to create a variable of a given type at runtime
where I won''t know the type until it actually executes. For example,

dim x as object = "hi"

x is declared as an object but x.gettype returns ''string'', so it knows it
contains a string.

If I want to create a variable "y" as the same type that variable x
contains how would I do that without having to iterate thru every possible
type? That is, for the above example I would want the equivalent of "dim
y as string" because x.gettype is string. Another example:

dim x as object = new MyClass

x.gettype would return ''MyClass'' so I would want the equivalent of "dim y
as myclass".

Although it''s not legal to do this "Dim y as x.gettype.name" that is
exactly what I want to do.

Is there anyway to accomplish this?

Thanks, John



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

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