我想判断一个类是否具有DataSource属性,然后对其赋值,下面有一段代码,不太明白

YiYanXiYin 2005-04-26 11:37:22
private void BoundControl_DataBound(object sender,System.EventArgs e)
{
if (HasParentControlCalledDataBinding) return;
Type type = sender.GetType();
_datasource = type.GetProperty("DataSource");
if (_datasource == null)
throw new NotSupportedException("分页控件要求表现控件必需包含一个DataSource。");
object data = _datasource.GetGetMethod().Invoke(sender,null);
_builder = Adapters[data.GetType()];
if (_builder == null)
throw new NullReferenceException("没有安装适当的适配器来处理下面的数据源类型:"+data.GetType());
_builder.Source = data;
}

_builder这个是什么类型?我仅仅需要对DataSource赋值即可

全文http://www.6to23.com/s11/s11d8/s11d8d1/2004421154352.htm

...全文
79 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
YiYanXiYin 2005-04-26
  • 打赏
  • 举报
回复
我现在就是要对DataSource赋值, 下面是我的代码
但对DataSource赋值时报“对象与目标类型不匹配。 ”,请问如何给DataSource赋值?

Type type = _DList.GetType();
System.Reflection.PropertyInfo _datasource = type.GetProperty("DataSource");
if (_datasource == null)
throw new NotSupportedException("分页控件要求表现控件必需包含一个DataSource属性。");
type.InvokeMember("DataSource",
BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.SetProperty, null, DSet.Tables[0], new Object[] {0});


3gold 2005-04-26
  • 打赏
  • 举报
回复
_builder是一个DataViewAdapter
3gold 2005-04-26
  • 打赏
  • 举报
回复
就是这部分判断的
_datasource = type.GetProperty("DataSource");//这里就得到数据源了
if (_datasource == null)
throw new NotSupportedException("分页控件要求表现控件必需包含一个DataSource。");
rachy 2005-04-26
  • 打赏
  • 举报
回复
private void BoundControl_DataBound(object sender,System.EventArgs e)
{
if (HasParentControlCalledDataBinding) return;
Type type = sender.GetType();
_datasource = type.GetProperty("DataSource");
if (_datasource == null)
throw new NotSupportedException("分页控件要求表现控件必需包含一个DataSource。");

程序到这里为止已经是可以判断出控件是否有datasource属性了
你下面要做的就是给datasource赋值
YiYanXiYin 2005-04-26
  • 打赏
  • 举报
回复
问题解决,散分

Type type = _DList.GetType();
System.Reflection.PropertyInfo _datasource = type.GetProperty("DataSource");
if (_datasource == null)
throw new NotSupportedException("分页控件要求表现控件必需包含一个DataSource属性。");
type.InvokeMember("DataSource",
BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.SetProperty, null,_DList, new Object[] { DSet.Tables[0]});
一小时 2005-04-26
  • 打赏
  • 举报
回复
up

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧