是否有现有的 C# 扩展方法库? 或分享您自己的

发布于 2024-07-23 02:11:30 字数 513 浏览 9 评论 0原文

可能的重复:
发布 C# .Net 的扩展好东西 (codeplex.com /扩展溢出)

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

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

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

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

Possible Duplicate:
Post your extension goodies for C# .Net (codeplex.com/extensionoverflow)

I'm fond of C# 3.0. One of my favorite parts is extension methods.

I like to think of extension methods as utility functions that can apply to a broad base of classes. I am being warned that this question is subjective and likely to be closed, but I think it's a good question, because we all have "boilerplate" code to do something relatively static like "escape string for XML" - but I have yet to find a place to collect these.

I'm especially interested in common functions that perform logging/debugging/profiling, string manipulation, and database access. Is there some library of these types of extension methods out there somewhere?

Edit: moved my code examples to an answer.
(Thanks Joel for cleaning up the code!)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

遥远的她 2024-07-30 02:11:30

这是 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));
}

Here's Jeff's ToDelimitedString written using String.Join:

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));
}
北城挽邺 2024-07-30 02:11:30

您可能喜欢MiscUtil

另外,很多人喜欢这个:

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

但是由于十次或更多中有九次我检查它是否为 null 或空,我个人使用这个:

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));
}

用于检查日期时间、布尔值或整数等值类型的默认值,或检查字符串等引用类型的 null 值。 它甚至可以作用于物体,这有点奇怪。

You might like MiscUtil.

Also, a lot of people like this one:

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

but since 9 times out of 10 or more I'm checking that it's not null or empty, I personally use this:

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

Finally, one I picked up just recently:

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

Works to check both value types like DateTime, bool, or integer for their default values, or reference types like string for null. It even works on object, which is kind of eerie.

少女的英雄梦 2024-07-30 02:11:30

这是我的几个:

// 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();
}

Here's a couple of mine:

// 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;
}

and a ToDelimitedString function:

// 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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文