Wrapper for code block
For example i have a datagrid and want to do something with it before databinding and after databinding, like so:
dgvTasksDocList.SaveLayoutToFile();
sTATUSCHANGESEXTENDEDBindingSource.DataSource = dt;
dgvTasksDocList.RestoreLayoutFromFile();
And i want t开发者_如何学Co add such code with all that cases when binding some sources. Is there an easy way to write such code before/after some action(Sure i can add this 2 lines, but may be there is some known way of doing such things)?
Seems to be a case for AOP - http://en.wikipedia.org/wiki/Aspect-oriented_programming.
For example with PostSharp you will be able to create an attribute PersistLayout
and apply it like this:
[PersistLayout]
public void BindDataSource(object dt)
{
sTATUSCHANGESEXTENDEDBindingSource.DataSource = dt;
}
Then Postsharp will extent source-code at compile-time to invoke your additional two-lines.
Other idea might be to invoke these two methods in sTATUSCHANGESEXTENDEDBindingSource.DataSource
property setter but this decision needs a lot more context to know.
public class MyDGV : DataGridView
{
public MyDGV()
{
DataSourceChanged += (s, e) => SaveLayoutToFile();
DataBindingComplete += (s, e) => RestoreLayoutFromFile();
}
}
Made it just with extension method, so now it looks like:
dgvTasksDocList.BindSourceAndSaveLayouts(sTATUSCHANGESEXTENDEDBindingSource, dt);
Or even:
dgvTasksDocList.BindSourceAndSaveLayouts(dt);
Because dgvTasksDocList.DataSource
can be casted to BindingSource
(sure i MUST provide that BindingSource
)
精彩评论