是否有现有的 C# 扩展方法库? 或分享您自己的
我喜欢 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是 Jeff 使用 String.Join 编写的 ToDelimitedString:
Here's Jeff's ToDelimitedString written using String.Join:
您可能喜欢MiscUtil。
另外,很多人喜欢这个:
但是由于十次或更多中有九次我检查它是否为 null 或空,我个人使用这个:
最后,我最近刚刚拿起的一个:
用于检查日期时间、布尔值或整数等值类型的默认值,或检查字符串等引用类型的 null 值。 它甚至可以作用于物体,这有点奇怪。
You might like MiscUtil.
Also, a lot of people like this one:
but since 9 times out of 10 or more I'm checking that it's not null or empty, I personally use this:
Finally, one I picked up just recently:
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.
这是我的几个:
和一个 ToDelimitedString 函数:
Here's a couple of mine:
and a ToDelimitedString function: