C#:这是ASP.NET项目的合理扩展方法吗?

C#:这是ASP.NET项目的合理扩展方法吗?,c#,asp.net,extension-methods,C#,Asp.net,Extension Methods,我最近发现了C#扩展方法,并写了这篇文章: /// <summary> /// Short-hand for setting the data source and binding it. /// </summary> /// <param name="me">The control to set and bind the data source of.</param> /// <param name="dataSource">The

我最近发现了C#扩展方法,并写了这篇文章:

/// <summary>
/// Short-hand for setting the data source and binding it.
/// </summary>
/// <param name="me">The control to set and bind the data source of.</param>
/// <param name="dataSource">The data source.</param>
public static void BindTo(this DataBoundControl me, IEnumerable dataSource)
{
    me.DataSource = dataSource;
    me.DataBind();
}
//
///用于设置数据源并绑定它的缩写。
/// 
///用于设置和绑定的数据源的控件。
///数据源。
公共静态void BindTo(此DataBoundControl me,IEnumerable数据源)
{
me.DataSource=数据源;
me.DataBind();
}

你们觉得怎么样?这是在专业ASP.NET项目中使用的合理扩展方法吗?

不太合适。在引入未知方法后,它将为您节省1个方法调用。而且你真的不需要一直设置绑定(取决于你怎么做,我想,我更喜欢在标记中这样做)


不,我不认为这是合适的。但是,我还是很反对一般的扩展方法,所以我显然有偏见,FWIW。

我认为这种扩展方法是有价值的,但只是作为一个更大的助手类的一部分。我曾经做过完全相同的事情——通常对于可编辑数据网格,我希望将每个单元格中的下拉列表绑定到特定的查找列表。它使代码更具可读性,特别是当您以编程方式生成databoundcontrol和datasource而不是在设计时进行所有操作时


但有一件事,我会将databoundcontrol参数名称从我改为其他名称,可能是boundControl

就像您将此参数命名为“me”:)