如何将类型转换为未知类型? [英] How to do type conversion to unknown type?

查看:359
本文介绍了如何将类型转换为未知类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将字符串(或对象)转换为基本类型。但我不知道

在运行时转换为的类型。


例如l有变量(假设它是一个int):

int unknownType = 0;

和一个字符串:

string str =" 123";


如果我知道类型是int我可以使用:

unknownType = Convert.ToInt32(str);或者int.Parse(str)等


我也可以使用Convert.ChangeType函数,但是这个函数

返回一个Object而不是一个原语再次需要强制转换的类型。


有没有办法在不使用switchType类型的开关

语句的情况下执行这种类型的转换?


-------

谢谢

Demorsy

I want to convert a string (or object) to a primitive type. But I don''t know
the type to convert to at run time.

For example l have variable (lets say its an int):
int unknownType = 0;
And a string:
string str = "123";

If I knew the type is int I could have used:
unknownType = Convert.ToInt32(str); or int.Parse(str) etc.

I also could have used the Convert.ChangeType function, but this function
returns an Object and not a primitive type that again need to cast.

Is there a way to perform this kind of casting without using the switch
statement on the Type of the unknownType type ?

-------
Thanks
Demorsy

推荐答案

Demorsy,


任何将一种类型转换为另一种类型并且不会在其名称中拼写

类型的方法(ChangeType vs. ToInt32)将返回Object。


但是内存中对象的实际类型将是你转换为
的类型。


我没有看到任何可能的情况,你所描述的情况可能会发生b $ b。


也许我如果你有错过的东西:
Demorsy,

Any method that converts one type to another and doesn''t spellout the the
type in its name (ChangeType vs. ToInt32) would return Object.

However the actuall type of the object in memory would be the type you
converted to.

I don''t see any possible situation where the situatuation you describe could
happen, though.

Maybe I miss something but if you have:
int unknownType = 0 ;
和一个字符串:
string str =" 123" ;;


你必须把* int *置于未知类型的前面,否则你无法编译。

构造如:

??? unknowType;在C#中是不可接受的。


与javascript不同,例如你在哪里修改变量,如:

var unknowType;

你不喜欢的地方在C#中不允许指定类型。


在所有.NET语言中如果你不想(或不能)指定你的类型/>
声明Object类型的变量:在这种情况下,你不应该使用ChangeType方法来解决
问题。但这也意味着你不希望
使用类型的东西而不是Object类接口提供的东西。


Stoitcho Goutsev(100) [C#MVP]


Demorsy <德***** @ discussions.microsoft.com>在消息中写道

news:71 ********************************** @ microsof t.com ...我想将字符串(或对象)转换为基本类型。但是我不知道在运行时转换为的类型。

例如我有变量(假设它是一个int):
int unknownType = 0;
和一个字符串:
string str =" 123" ;;

如果我知道类型是int我可以使用:
unknownType = Convert.ToInt32(STR);或者int.Parse(str)等

我也可以使用Convert.ChangeType函数,但是这个函数
返回一个Object而不是再次需要强制转换的基本类型。 br />
有没有办法在不使用未知类型类型的开关
语句的情况下执行这种类型的转换?

-------
谢谢
Demorsy
int unknownType = 0;
And a string:
string str = "123";
You have to put *int* infront of unknownType otherwise you cannot compile.
Constructions like:
??? unknowType; are not acceptable in C#.

Unlike javascript for example where you decalre variables like:
var unknowType;
where you don''t specify the type are not allowed in C#.

In all .NET languages If you don''t want to (or can''t) specify the type you
declare the variable of type Object: in this case you shouldn''t have
problems using ChangeType method. But that also means you that don''t want to
use anything more from the type than the Object class interface provides.

Stoitcho Goutsev (100) [C# MVP]

"Demorsy" <De*****@discussions.microsoft.com> wrote in message
news:71**********************************@microsof t.com...I want to convert a string (or object) to a primitive type. But I don''t
know
the type to convert to at run time.

For example l have variable (lets say its an int):
int unknownType = 0;
And a string:
string str = "123";

If I knew the type is int I could have used:
unknownType = Convert.ToInt32(str); or int.Parse(str) etc.

I also could have used the Convert.ChangeType function, but this function
returns an Object and not a primitive type that again need to cast.

Is there a way to perform this kind of casting without using the switch
statement on the Type of the unknownType type ?

-------
Thanks
Demorsy



我不是那个创建unknownType实例的人。

其实我收到一个XML字符串,其中包含要在DataSet中设置的真实

变量的值。

我不知道在开发时知道变量类型,但是我可以使用GetType()方法在运行时知道它。所以现在我知道了类型,我需要设置

其值在XML字符串中找到。


所以你看,我需要转换一个字符串对于一个未知的类型。


.NET应该支持,但我没有找到如何做到这一点。


关于如何做的任何想法这样做?

------

谢谢

Demorsy
I''m not the one that create the instance of the unknownType.
Actually I''m receiving an XML string that contain values to be set on a real
variable that are located in a DataSet.
I do not know the variable type at development time, but I can know it at
run time using the GetType() method. So now I know the type and I need to set
its value as found in the XML string.

So you see, I need to convert a string to an unknown type.

The .NET should support that, yet I did not find how to do that.

Any idea on how to do that?
------
Thanks
Demorsy


Demorsy,


我想我现在看到你的问题是什么。不幸的是,AFAIK没有

转换可以帮助你解决这个问题。正如我所说的所有通用数据

转换方法可能存在返回类型Object。


当您填充数据表时,您需要创建并添加一个DataRow中。即使

如果你输入了DataRow它继承了DataRow类无类型索引器

仍然可用。


我相信你可以做。


myDataRow [" some column name"] = Convert.ChangeType(value,< xxxx> .GetType());


HTH

Stoitcho Goutsev(100)[C#MVP]


" Demorsy" <德***** @ discussions.microsoft.com>在消息中写道

news:28 ********************************** @ microsof t.com ...
Demorsy,

I think I see now what your problem is. Unfortunately, AFAIK there is no
conversion that can help you with this. As I said all generic data
conversion methods that could exist would have return type Object.

When you populate the data table you need to create and add a DataRow. Even
if you have typed DataRow it inherits from the DataRow class untyped indexer
is still available.

I believe you can do.

myDataRow["some column name"] = Convert.ChangeType(value, <xxxx>.GetType());

HTH
Stoitcho Goutsev (100) [C# MVP]

"Demorsy" <De*****@discussions.microsoft.com> wrote in message
news:28**********************************@microsof t.com...
我不是那个创建unknownType实例的人。
实际上我收到的XML字符串包含要设置的值
真实的
变量位于DataSet中。
我不知道开发时的变量类型,但我可以在运行时使用GetType()方法知道它。所以现在我知道了类型,我需要设置它在XML字符串中找到的值。

所以你看,我需要将字符串转换为未知类型。

.NET应该支持,但我没有找到如何做到这一点。

任何想法如何做到这一点?

------
谢谢
Demorsy
I''m not the one that create the instance of the unknownType.
Actually I''m receiving an XML string that contain values to be set on a
real
variable that are located in a DataSet.
I do not know the variable type at development time, but I can know it at
run time using the GetType() method. So now I know the type and I need to
set
its value as found in the XML string.

So you see, I need to convert a string to an unknown type.

The .NET should support that, yet I did not find how to do that.

Any idea on how to do that?
------
Thanks
Demorsy



这篇关于如何将类型转换为未知类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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