CYQ. Data lightweight Data access layer (8) custom Data tables to bind Common Data Controls (medium)

Source: Internet
Author: User
Tags reflector

After the previous section, we began to look for the binding method.

First, let's recall what we usually bind to a data control: List <entity class>, DataTable, DataSet, DataView, etc,

The statement is as follows:

Xxxx. DataSource = List <entity class>... and so on

XXxx. DataBind ();

So we can start with these things, or we can't find out where to go.

Performanceis going to give value in the east, so let's start with reflector.exe to see if there is anything in it first.

Press F3 to search for Repeater. Of course, you can also search for other controls such as GridView. You can search for DataSource.

OK. After locating the Repeater, expand the DataSource attribute. The source code is as follows:

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 ();
}
}

 

Yo, yoyo ~~ Value is IListSource or value is IEnumerable. If not, an exception is thrown.

I suddenly remembered that I had thrown an exception in the past, but it was still Chinese. I suddenly wanted to see what the abnormal Chinese version was like.

So I randomly found a page, pulled a GridView, and wrote the following lines of code in the background:

Object value = "hello ";
GridView1.DataSource = value;
GridView1.DataBind ();

Come on, give me an error, run .....

Brush .. Something came out:

Item
H
E
L
L
O

Sun, this is awesome. I also gave it to the hacker. I searched for the object from reflector.exe and found that it did not inherit from IListSource or IEnumerable.

Sun, change object value = "hello" to object value = 1, try, run, brush, expect Chinese Yellow exception finally arrived

"The data source type is invalid. It must be IListSource, IEnumerable, or IDataSource. "

No, trouble. Add yellow directly. Search String from reflector.exe,

I found that the String actually inherits the IEnumerable. Alas...

The problem is getting worse, and the focus is onIListSourceAndIEnumerableAs long as our custom MDataTable implements one of the interfaces, everything is fine.

Use reflector.exe to check IListSource:

Public interface IListSource
{
// Methods
IList GetList ();

// Properties
Bool ContainsListCollection {get ;}
}

Oh, it turns out that this interface returns an IList, so our custom MDataTable also seems to have a List interface. No wonder we can bind it directly,

But it is bound to the object attributes of some classes. It seems that the conjecture may be similar to those of the List <object class> class.

Since we simulate the able in a simplified way, we can find the DataTable and use reflector and exe to search for the DataTable and locate it,

Surprisingly, DataTable actually inherits the IListSource interface, which is not just like me. It can be bound as soon as possible, and I cannot.

One sun ..

Open the GetList () method of implementation. After a circle is taken, a DataView is returned,

Open DataView and find that it still inherits IEnumerable.

Alas, if you don't have to worry about it, you can simply Mount IEnumerable:

Public interface IEnumerable
{
[DispId (-4)]
IEnumerator GetEnumerator ();
}

I inherited the interface first. I want to implement the interface method. It is dizzy and won't be written. I can't write a program and throw an exception ....

 

Another flash, since DataView implements that interface, let's see how people implement it. it's almost the same as imitating it:

Public IEnumerator GetEnumerator ()
{
DataRowView [] array = new DataRowView [this. RowViewCache. Count];
This. RowViewCache. CopyTo (array, 0 );
Return array. GetEnumerator ();
}

 

Oh, it turns out that a row array was created and copied again. Then, the GetEnumerator () method of the array was called,

I finally found a bright spot and copied it to the path...

Continue in the next section ....

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.