如何进行动态的数据类型转换?
public void DataBind(WebControl control_c)
{
string type_s = control_c.GetType().ToString();
string [] types=type_s.Split( '. ');
type_s=types[types.Length-1];
switch (type_s)
{
case "DataGrid ":
DataGrid c = (DataGrid)control_c;
break;
case "GridView ":
GridView c = (GridView)control_c;
break;
case "Repeater ":
Repeater c=(Repeater)control_c;
break;
default:
throw System.Exception;
}
control_c.DataSource = this.DataSource;
}
以上代码是错的,一直没有找到好的解决方法,高手们快来解答吧!
我的意思是:control_c有可能是以上类型,有没有办法获取它实际的类型,并转换过来!
[解决办法]
public void DataBind(WebControl control_c) { System.Reflection.PropertyInfo p = control_c.GetType().GetProperty( "DataSource "); p.SetValue(control_c, 数据源, null); }