【问题标题】:Is there an existing library of extension methods for C#? or share your own [duplicate]是否有现有的 C# 扩展方法库?或分享你自己的[重复]
【发布时间】:2009-05-28 21:40:31
【问题描述】:

可能重复:
Post your extension goodies for C# .Net (codeplex.com/extensionoverflow)

我喜欢 C# 3.0。我最喜欢的部分之一是扩展方法。

我喜欢将扩展方法视为可以应用于广泛的类基础的实用函数。我被警告说这个问题是主观的,可能会被关闭,但我认为这是一个很好的问题,因为我们都有“样板”代码来做一些相对静态的事情,比如“XML 的转义字符串”——但我还没有找到收集这些的地方。

我对执行日志记录/调试/分析、字符串操作和数据库访问的常用函数特别感兴趣。是否有一些此类扩展方法的库?

编辑:将我的代码示例移至答案。 (感谢 Joel 清理代码!)

【问题讨论】:

  • 请将问题的“答案”部分作为答案发布。

标签: c# extension-methods boilerplate


【解决方案1】:

你可能会喜欢MiscUtil

还有很多人喜欢这个:

public static bool IsNullOrEmpty(this string s)
{
    return s == null || s.Length == 0;
}

但由于 10 次或更多次中有 9 次我正在检查它是否 为空或为空,所以我个人使用这个:

public static bool HasValue(this string s)
{
    return s != null && s.Length > 0;
}

最后,我最近捡到的一个:

public static bool IsDefault<T>(this T val)
{
    return EqualityComparer<T>.Default.Equals(val, default(T));
}

可以检查 DateTime、bool 或 integer 等值类型的默认值,或者检查字符串等引用类型是否为 null。它甚至适用于物体,这有点怪异。

【讨论】:

  • 这并不重要,但你为什么不只是“返回 string.IsNullOrEmpty(s)”?
  • 你也可以这样做。出于某种原因,我喜欢这种方式,但我都见过。
  • 实际上,我个人使用了一些不同的东西,因为 10 次中有 9 次我检查它是否为空或空(有值),我的看起来更像:public static bool HasValue (this string s) { return s != null && s.Length > 0; }
  • 如果你想要语法 strValue.IsNullOrEmpty() ,你不会写成: public static bool IsNullOrEmpty(this string s) { return string.IsNullOrEmpty(s); }
  • 叹息:看看我对 Max 评论的回复。
【解决方案2】:

这是我的几个:

// returns the number of milliseconds since Jan 1, 1970 (useful for converting C# dates to JS dates)
public static double UnixTicks(this DateTime dt)
{
    DateTime d1 = new DateTime(1970, 1, 1);
    DateTime d2 = dt.ToUniversalTime();
    TimeSpan ts = new TimeSpan(d2.Ticks - d1.Ticks);
    return ts.TotalMilliseconds;
}

还有一个 ToDelimitedString 函数:

// apply this extension to any generic IEnumerable object.
public static string ToDelimitedString<T>(this IEnumerable<T> source, string delimiter, Func<T, string> action)
{
    if (source == null)
    {
        throw new ArgumentException("Source can not be null.");
    }

    if (delimiter == null)
    {
        throw new ArgumentException("Delimiter can not be null.");
    }

    string strAction = string.Empty;
    string delim = string.Empty;
    var sb = new StringBuilder();

    foreach (var item in source)
    {
        strAction = action.Invoke(item);

        sb.Append(delim);
        sb.Append(strAction);
        delim = delimiter;
    }
    return sb.ToString();
}

【讨论】:

  • 请注意,我的清理与您的行为不完全匹配:如果 strAction 为空,您没有附加任何内容,这仍会在其中放置一个空项目。
  • String.Join 在这种情况下更合适——我很快就会在这里发布一个例子。
  • 已发布示例。我更喜欢在可用时使用库方法,我想我会展示一个示例。
  • 谢谢大家。我看到我的问题是完全重复的,甚至还有一个 codeplex 项目 extensionoverflow!太棒了!
【解决方案3】:

这是 Jeff 使用 String.Join 编写的 ToDelimitedString:

public static string ToDelimitedString<T>(this IEnumerable<T> source, string delimiter, Func<T, string> action) {
    // guard clauses for arguments omitted for brevity

    return String.Join(delimiter, source.Select(action));
}

【讨论】:

  • 你真的需要在那里调用 ToArray 吗?我认为最好把它留给 string.Join 谁会在内部这样做
  • @nawfal - 你是对的 - 这里不需要调用 .ToArray。已编辑。
猜你喜欢
  • 2014-09-15
  • 1970-01-01
  • 2017-07-27
  • 1970-01-01
  • 2022-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多