![]() | CYQ.Data componentsCYQ.Data support multi-database application [Txt,Xml,Access, MSSQL, Oracle,SQLite,MySql], help easily and quickly to develop your project |
MDataTable 原理解析 数据控件绑定-中篇
Principles |
|
|
| #TopicOwner | ||||||
继上一节之后,我们开始寻找绑定之法 先是一回想,我们平常是拿什么绑定到数据控件的:List<实体类>,DataTable,DataSet,DataView之类的, 而写法也就这么个样: xxxx.DataSource=List<实体类>....等 XXxx.DataBind(); 于是,我们就从这些东西入手了,要不然也措不着头脑该往哪儿找 DataSource这东西要给赋值,那我们打开reflector.exe看看这里面究竟有点什么东西先 按下F3,搜索Repeater,当然也可以搜其它象GridView之类的其它控件,反正搜有DataSource这东西的就行了 OK,定位Repeater之后,展开DataSource属性,看源码如下: ![]() ![]() [Bindable(true), WebSysDescription("BaseDataBoundControl_DataSource"), WebCategory("Data"), DefaultValue((string) null), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] public virtual object DataSource { get { return this.dataSource; } set { if (((value != null) && !(value is IListSource)) && !(value is IEnumerable)) { throw new ArgumentException(SR.GetString("Invalid_DataSource_Type", new object[] { this.ID })); } this.dataSource = value; this.OnDataPropertyChanged(); } }
哟,yoyo~~value is IListSource 或者value is IEnumerable,如果不是这两种类型,还抛异常 突然记起来,以前也抛过异常的,还是中文的,突然想看看这个异常的中文版是啥样的 于是,随便找了一个页面,拉了一个GridView进去,后台写上这么几行代码: object value="hello";
GridView1.DataSource = value; GridView1.DataBind(); 来吧,给我尽情的报错吧,运行。。。。。 刷。。出来了这么点东西:
太阳,这么牛B,还给拆成字符给我列表下来了,从reflector.exe搜索object,发现没继承自IListSource 或IEnumerable。 太阳,把object value="hello",改成object value=1,一试,运行,刷,期待的中文黄色异常竟然终于来了 "数据源的类型无效。它必须是 IListSource、IEnumerable 或 IDataSource。" 不截图了,麻烦。直接加黄色好了。突然脑子一闪,从reflector.exe搜索String, 发现竟然String还真的继承了IEnumerable这东西。唉。。。 于上问题开始白热化了,焦点就在IListSource和IEnumerable上了,只要让我们的自定义MDataTable实现其中之一的接口,就万事大吉了。 先用reflector.exe看一下IListSource这东西: public interface IListSource
{ // Methods IList GetList(); // Properties bool ContainsListCollection { get; } } 哦!!! 原来这接口就是返回一个IList,那我们的自定义的MDataTable好像也有List接口了,怪不得直接绑定, 却绑定了一个些类的实体属性,看来猜想有可能和那些List<实体类>一类的就绑定都差不多了. 既然我们是简化的模仿DataTable,那就从DataTable找起好了,还是用reflector,exe搜索DataTable并定位, 惊人发现,DataTable竟然继承了IListSource接口,那它不是和我的一个样,咋它就能绑定就正常,我的就不能。 太阳一个。。 打开实现的GetList()方法一看,转了一圈,返回了一个DataView, 再打开DataView,发现原来还是继承了IEnumerable 唉,不折腾了,直接就上马IEnumerable好了: public interface IEnumerable
{ [DispId(-4)] IEnumerator GetEnumerator(); } 先把接口继承了,要实现接口方法了,晕,不会写,总不能写个抛异常吧。。。。
又一闪,既然DataView实现那个接口,看看人家怎么实现,照着模仿一下应该就差不多了吧: public IEnumerator GetEnumerator()
{ DataRowView[] array = new DataRowView[this.RowViewCache.Count]; this.RowViewCache.CopyTo(array, 0); return array.GetEnumerator(); } oh,竟然原来是搞了一份行数组,又来copy一下,之后返回调用数组的GetEnumerator()方法, 终于发现眼前一片光明,抄到小路了... 下节继续.... ![]() |
Post Comment
Bulletin
Article Search
Categories
- Platform for dynamic (20)
- Feedback (9)
- Guide (33)
- Principles (19)
- Project-Case (8)
- Business & Buy (2)
- Technology exchange (45)
New Article
- CYQ.Data Components Getting Started Guide [Part 5]-[MProc Execute Stored Procedures or SQL]
- CYQ.Data Components Getting Started Guide [Part 4]-[MAction Insert Delete Update]
- CYQ.Data Components Getting Started Guide [Part 3]-[MAction Get And Set Value]
- CYQ.Data Components Getting Started Guide [Part 2]-[MAction Data Query- Fill And Select]
- CYQ.Data Components Getting Started Guide [Part 1]
New Comment
- When some one searches for his necessary thing, therefore he/she wishes to be available that in detail, so that thing is maintained over here.
- This is my first time pay a quick visit at here and i am in fact happy to read everthing at alone place.
- I truly appreciate this blog article.Really thank you! Cool.
- please pay a visit to the web sites we follow, like this one particular, as it represents our picks in the web
- Really enjoyed this post.Really thank you!
- Really enjoyed this article.Really looking forward to read more. Great.
- poker bonuses What are the norms of copyright of web content? How as it different from Patent?
- Wow! Thank you! I permanently needed to write on my blog something like that. Can I implement a fragment of your post to my site?
- This website was how do I say it? Relevant!! Finally I ave found something that helped me. Cheers!
- I was reading through some of your content on this internet site and I believe this web site is very informative ! Continue posting.