net 参数 方法问题
我要写一个方法,作用是把数据绑定到指定控件
public void bangding(Repeater rep, string select)
{
OleDbCommand com = new OleDbCommand(select, con);
open();
rep.DataBind = com.ExecuteReader();
rep.DataSource();
close();
}
但像这种控件有很多,GridView,DataList等不想用重载。只想写一遍就搞定。请问怎么办
果然是高人,学习一下
public void bangding(object DataControl, string select)
{
OleDbCommand com = new OleDbCommand(select, con);
open();
Type objType = DataControl.GetType();
System.Reflection.PropertyInfo DataSource = objType.GetProperty("DataSource");
DataSource.SetValue(DataControl, com.ExecuteReader(), null);
System.Reflection.MethodInfo DataBind = objType.GetMethod("DataBind");
DataBind.Invoke(DataControl, null);
close();
}
把第一个参数改成一个object类型的,把数据绑定控件通册厅过第一州洞隐个参数传进来,颤早然后使用反射机制来实现。