见过的扩展方法的最佳或最有趣的用途是什么?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (59)

我开始真的很喜欢延伸方法......我想知道是否有人发现她已经绊倒了一个真正吹嘘他们的头脑,或者只是发现了聪明。

我今天写的一个例子:

public static IEnumerable<int> To(this int fromNumber, int toNumber) { while (fromNumber < toNumber) { yield return fromNumber; fromNumber++; } }

这允许一个for循环写成一个foreach循环:

foreach (int x in 0.To(16)) {
    Console.WriteLine(Math.Pow(2, x).ToString());
}
提问于
用户回答回答于

最近一段时间以来我一直在玩这个:

public static IDisposable Tag(this HtmlHelper html, string tagName)
{
    if (html == null)
        throw new ArgumentNullException("html");

    Action<string> a = tag => html.Write(String.Format(tag, tagName));
    a("<{0}>");
    return new Memento(() => a("</{0}>"));
}

用途如下:

using (Html.Tag("ul"))
{
    this.Model.ForEach(item => using(Html.Tag("li")) Html.Write(item));
    using(Html.Tag("li")) Html.Write("new");
}

类:

public sealed class Memento : IDisposable
{
    private bool Disposed { get; set; }
    private Action Action { get; set; }

    public Memento(Action action)
    {
        if (action == null)
            throw new ArgumentNullException("action");

        Action = action;
    }

    void IDisposable.Dispose()
    {
        if (Disposed)
            throw new ObjectDisposedException("Memento");

        Disposed = true;
        Action();
    }
}

并完成依赖关系:

public static void Write(this HtmlHelper html, string content)
{
    if (html == null)
        throw new ArgumentNullException("html");

    html.ViewContext.HttpContext.Response.Write(content);
}
用户回答回答于

public static String ToCSV(this DataTable dataTable) { return dataTable.ToCSV(null, COMMA, true); } public static String ToCSV(this DataTable dataTable, String qualifier) { return dataTable.ToCSV(qualifier, COMMA, true); } private static String ToCSV(this DataTable dataTable, String qualifier, String delimiter, Boolean includeColumnNames) { if (dataTable == null) return null; if (qualifier == delimiter) { throw new InvalidOperationException( "The qualifier and the delimiter are identical. This will cause the CSV to have collisions that might result in data being parsed incorrectly by another program."); } var sbCSV = new StringBuilder(); var delimiterToUse = delimiter ?? COMMA; if (includeColumnNames) sbCSV.AppendLine(dataTable.Columns.GetHeaderLine(qualifier, delimiterToUse)); foreach (DataRow row in dataTable.Rows) { sbCSV.AppendLine(row.ToCSVLine(qualifier, delimiterToUse)); } return sbCSV.Length > 0 ? sbCSV.ToString() : null; } private static String ToCSVLine(this DataRow dataRow, String qualifier, String delimiter) { var colCount = dataRow.Table.Columns.Count; var rowValues = new String[colCount]; for (var i = 0; i < colCount; i++) { rowValues[i] = dataRow[i].Qualify(qualifier); } return String.Join(delimiter, rowValues); } private static String GetHeaderLine(this DataColumnCollection columns, String qualifier, String delimiter) { var colCount = columns.Count; var colNames = new String[colCount]; for (var i = 0; i < colCount; i++) { colNames[i] = columns[i].ColumnName.Qualify(qualifier); } return String.Join(delimiter, colNames); } private static String Qualify(this Object target, String qualifier) { return qualifier + target + qualifier; }

你可以这样:

someDataTable.ToCSV(); //Plain old CSV
someDataTable.ToCSV("\""); //Double quote qualifier
someDataTable.ToCSV("\"", "\t"); //Tab delimited

扫码关注云+社区

领取腾讯云代金券