.net3.0 extension methods for interface
.net得扩展方法用在接口上,能使得用户不需要实现,额外获得一些方法:
publicinterfaceICollectionControlVisualInformationHelper
{
int VisualIndexofCurrentItem(Control targetControl, ICollectionView view);
int CountOfVisualItems(Control targetControl, ICollectionView view);
int VisualIndexConvertToDataItemIndex(int visualIndex);
void DoSomethingAfterMoveUpDown(Control targetControl, ICollectionView view);
}
internalstaticclassICollectionControlVisualInformationHelperExtensionMethod
{
publicstaticint VisualIndexOfPreviousItem(thisICollectionControlVisualInformationHelper helper,Control targetControl, ICollectionView view)
{
return helper.VisualIndexConvertToDataItemIndex(helper.VisualIndexofCurrentItem(targetControl,view) - 1);
}
publicstaticint VisualIndexOfNextItem(thisICollectionControlVisualInformationHelper helper, Control targetControl, ICollectionView view)
{
return helper.VisualIndexConvertToDataItemIndex(helper.VisualIndexofCurrentItem(targetControl, view) + 1);
}
}
这样使得用户实现4个方法,却获得了接口的六个方法。