在线等! asp.net c#想用参数设定强制转换类型 怎么写

用函数传一个参数作转换类型,比如说
Control ctrl = (Control)Page.LoadControl("aaa.ascx");
Textbox1.Text = ((Label)ctrl).ToString();
Textbox2.Text = ((ListBox)ctrl).ToString();

想问下有没可能写一个函数fn实现
Textbox1.Text = fn(ctrl, Label);
Textbox2.Text = fn(ctrl, ListBox);

下面的代码是我在项目中使用 的
TextBox控件类型,customer窗体名,txtValue控件名
GetControl<TextBox>(customer, "txtValue");

/// <summary>
/// 只遍历控件的子控件,不遍历孙控件
///当控件有子控件时,需要用递归的方法遍历,才能全部列出控件上的控件
/// </summary>
/// <typeparam name="T">要匹配的控件类型</typeparam>
/// <param name="control">要遍历的了控件</param>
/// <param name="controlsName">要匹配的控件名</param>
/// <returns></returns>
public static Control GetControl<T>(Control control, string controlsName)
{
if (control == null) return null;
Control _control;
for (int i = 0; i < control.Controls.Count; i++)
{
_control = control.Controls[i];
if (_control == null) return null;
if (_control.Name == controlsName && _control is T)
return _control;
if (_control.HasChildren)
{
_control = GetControl<T>(_control, controlsName);
if (_control != null)
return _control;
}
}
return null;
}
/// <summary>
/// 遍历窗体所有控件
/// </summary>
/// <typeparam name="T">要匹配的控件类型</typeparam>
/// <param name="form">窗体名</param>
/// <param name="controlsName">要匹配的控件名</param>
/// <returns></returns>
public static Control GetControl<T>(Form form, string controlsName)
{
Control _Control = null;
for (int i = 0; i < form.Controls.Count; i++)
{
_Control = GetControl<T>(form.Controls[i], controlsName);
if (_Control != null)
return _Control;
}
return null;
}
使用方法:
Control _control;
_control = GetControl<TextBox>(customer, "txtValue");
if(_control!=null)
((TextBox) _control).Text = "text";
_control = GetControl<ComboBox>(customer, "ddl");
if (_control != null)
((ComboBox)_control).SelectedIndex = 0;追问

像你说的这个例子,
_control = GetControl(customer, "txtValue");
我希望能做到
_control = GetControl(customer, "TextBox", "txtValue");
怎么弄?

追答

_control = GetControl(customer, "txtValue");
和下面的有什么区别呢?能实现相同的效果不就行了,这用了泛型,会更安全些效率也高,如果用下面的话你只有用if或Swith来判断啦建议用上面的,可以是任何控件,
_control = GetControl(customer, "TextBox", "txtValue");

追问

有区别,因为我是通过自定义控件传递参数,只能传字符串...
希望能找到一个通过字符串就能实现强制转换的方法,比如说,通过字符串"Label"就能把控件强制转换成Label.

追答

贴一下代码看下

追问

代码和这个文档第4,5页的差不多,就是多了个参数
http://wenku.baidu.com/view/76d6d98683d049649b6658cf.html
aspx文件里面

cs文件里面用this.FirstWebControl1.DataType就得到"Label"字符串

追答

不知道你这个用户控件是有什么作用,
string GetControlText(string controlName,string ControlType)
{
Control ctrl = (Control)Page.LoadControl("aaa.ascx");
switch(ControlType)
case:"Lable":return ((Lable) ctrl).ToString();
case:...
case:...
}

温馨提示:内容为网友见解,仅供参考
第1个回答  2011-11-22
弱弱的看了一下别人的回答和你的追问。。。
我想问一下你,你可以仅仅通过一个字符串来反射获得一个类型吗?如果不行,你就实现不了你想要的功能,反之,如果可以,你必须满足2个条件,1、这个字符串是这个类型的FullName;2、你在项目中引用了这个类型的程序集。。。
你只有一个办法,就是定义一个字典,创建KeyValuePair<string, Type>这样的键值对来限定目标转换类型,这样才能实现动态转换。。。
除此以外,我可以很明确的告诉你,类型转换只有3种可能实现的途径:1、装箱拆箱;2、对象之间有继承关系;3、你已经重写了这2个对象之间的显式转换(explicit operator)和隐式转换(implicit operator)。
第2个回答  2011-11-22
用反射来做,不过相当复杂,你问这个问题有点像VB,似乎没有太大的意义,如果你执意要做:给你一个简单的思路吧
/// <summary>
/// DataTable转化为List通用类
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public static List<T> ConvertToList(DataTable dt)
{
List<T> ts = new List<T>();
foreach (DataRow dr in dt.Rows)
{
T t = new T();// constraint
//获得当前对象的所有属性
System.Reflection.PropertyInfo[] propertys = t.GetType().GetProperties();
foreach (System.Reflection.PropertyInfo property in propertys)
{
foreach (DataColumn dc in dt.Columns)
{
//匹配属性和列
if (dc.ColumnName.Replace("_", "").ToLower().Trim() == property.Name.ToLower().Trim())
{
//判断属性的写权限
if (property.CanWrite)
{
if (dr[dc] != DBNull.Value)
{
property.SetValue(t, dr[dc], null);
}
}
}
}

}
ts.Add(t);
}
return ts;
}
第3个回答  2011-11-22
去看一下泛型函数或者泛型方法。追问

看了一下,好像没有讲如何在函数传递变量类型啊?能否具体一些

相似回答