请建议最好的方法,用于类型转换 [英] please suggest the best method, for type casting

查看:63
本文介绍了请建议最好的方法,用于类型转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友,
我正在尝试将对象obj类型转换为数据类型(如数据集,toolstriptextbox或toolstripcombobox)之一.根据obj的类型.您能建议我如何动态地将obj转换为这些类型之一.最好用什么代替?",以便将obj轻松转换为最合适的类型.

Hi Dear Friends,
I am trying to cast object obj type, into one of data types like dataset, toolstriptextbox or toolstripcombobox. according to the type of obj. Can you please suggest how can I dynamically cast the obj in one of these types. what is the best thing in place of ''?'' so the obj is easily casted to the best suitable type.

if (obj is DataSet || obj is ToolStripTextBox || obj is ToolStripComboBox )
                  {
                      ListOfControls.Add(obj);
                      serializablearray.Add(((?)obj).Name);
                  }


谢谢

推荐答案

数据集不是控件.尽可能使用强类型列表.使用as关键字转换您的对象,以便您可以将它们作为强类型对象插入.您的列表是半没用的,这些项甚至都没有通用的基类,因此它们仍然需要进行强制转换.
A dataset is not a control. Use strongly typed lists where ever possible. Use the as keyword to convert your objects so you can insert them as a strongly typed object. Your list is semi useless, those items don''t even have a common base class, so they still need casting to do anythign with.


如果您只想存储类型名称,只需这样做:

If you simply want to store the type name, just do this:

ListOfControls.Add(obj);
serializablearray.Add(obj.GetType().Name);



如果要包括完全限定的名称空间路径,可以选择使用obj.GetType().FullName.



You can optionally use obj.GetType().FullName if you want to include the fully qualified namespace path.


这篇关于请建议最好的方法,用于类型转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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