我正在寻找一个库或源代码,它提供了诸如检查空参数之类的守护方法。很明显,这是相当简单的构建,但我想知道是否有任何.NET的。基本的谷歌搜索没有透露太多。.NET Guard类库?
回答
有CuttingEdge.Conditions。从页面用例:
public ICollection GetData(Nullable<int> id, string xml, ICollection col)
{
// Check all preconditions:
id.Requires("id")
.IsNotNull() // throws ArgumentNullException on failure
.IsInRange(1, 999) // ArgumentOutOfRangeException on failure
.IsNotEqualTo(128); // throws ArgumentException on failure
xml.Requires("xml")
.StartsWith("<data>") // throws ArgumentException on failure
.EndsWith("</data>"); // throws ArgumentException on failure
col.Requires("col")
.IsNotNull() // throws ArgumentNullException on failure
.IsEmpty(); // throws ArgumentException on failure
// Do some work
// Example: Call a method that should not return null
object result = BuildResults(xml, col);
// Check all postconditions:
result.Ensures("result")
.IsOfType(typeof(ICollection)); // throws PostconditionException on failure
return (ICollection)result;
}
另外一个不错的方法,这是不是在一个库包装,但可以很容易地,on Paint.Net blog:
public static void Copy<T>(T[] dst, long dstOffset, T[] src, long srcOffset, long length)
{
Validate.Begin()
.IsNotNull(dst, "dst")
.IsNotNull(src, "src")
.Check()
.IsPositive(length)
.IsIndexInRange(dst, dstOffset, "dstOffset")
.IsIndexInRange(dst, dstOffset + length, "dstOffset + length")
.IsIndexInRange(src, srcOffset, "srcOffset")
.IsIndexInRange(src, srcOffset + length, "srcOffset + length")
.Check();
for (int di = dstOffset; di < dstOffset + length; ++di)
dst[di] = src[di - dstOffset + srcOffset];
}
我用它在my project,你可以借代码从那里。
鉴于微软的Code Contracts与.NET 4.0问世,如果可能的话,我会尽量找到一个兼容的,如果没有的话,自己写。当你升级到.NET 4.0时(最终),迁移将变得更加容易。
有几种方法可以使用。
我最喜欢的是使用Aspect Oriented Programming。退房PostSharp。
您还可以看看规格#,延伸到C#
在4.0中,你将有一个全功能的合同库。
最后,我的一个collegue想出了一个非常有用的后卫库: http://blueonionsoftware.com/blog.aspx?p=ca49cb62-7ea2-43c5-96aa-91d11774fb48
我不知道有什么可购得。这种类型的代码在&模式下对企业库有一些支持。在CodePlex上也有很多开源项目可以做到这一点(不同程度):http://www.codeplex.com/Project/ProjectDirectory.aspx?ProjectSearchText=validation。
大多数情况下,这些类型的库最终都是自定义编写的,并停留在使用它们的公司内部。
.NET 4.0中提供了对基于Spec#的Code Contracts的支持机制。
我最近写了一篇关于警卫班后(已没有发现任何信息其一):http://ajdotnet.wordpress.com/2009/08/01/posting-guards-guard-classes-explained/
我还出版了各保护类的实现(随意直接使用这些代码,或将其调整到您的需求):ajdotnet.wordpress.com/guard-class/
关于.NET 4.0中Guard类和代码契约之间的关系(Spec#的后继者),请看下面的帖子:www.leading -edge-dev.de/?p=438
(遗憾的是分段链接,网站只允许一个链接...)
HIH, AJ.NET
安装netfx-guard nuget包。您还会得到代码片段notnull和notempty,它的执行速度与您的手动检查一样快。
- 1. 错误 - 无法加载'guard/rails-assets'或''查找类Guard :: RailsAssets(Rails 4/zeus/guard)
- 2. .NET Framework 4.6.2中的目标.NET核心类库类库
- 3. 的.NET Framework 4.6类库.NET的核心
- 4. VB .NET(.NET 2.0)类库配置
- 5. SerializationException在LoadFrom'ed .NET类库
- 6. .NET类库中支持WriteFileGather
- 7. .NET类库DLL刷新
- 8. VB调用.NET类库(dll)
- 9. 类库.NET标准和mscorlib.dll
- 10. 类库编译.DLL .NET
- 11. VBA - 使用.NET类库
- 12. .Net共享类库配置
- 13. typescript generic type guard
- 14. Better Guard(Spork/RSpec)
- 15. Data Guard Oracle 11g
- 16. 问题,而在标准的.Net类库引用.net库中
- 17. Angular 2 Route Guard Confiability
- 18. 类库和.NET核心类库模板有什么区别?
- 19. 从.NET类库中加载类库中的资源
- 20. 参考.NET类库的有用的Silverlight类库
- 21. .Net类库:线程的责任
- 22. .NET - 使用类库存储App.config
- 23. 在类库项目中使用ActionFilterAttribute(.NET)
- 24. DeltaXML Diff类似于.Net的库吗?
- 25. 用于VB的.NET类型库6
- 26. 在其他.NET类库中使用DbContext
- 27. 扩展用于WCF的.NET 2.0类库
- 28. Microsoft.AspNetCore.TestHosts不能在.NET 1.6标准类库
- 29. 可移植类库和.NET Framework 4.6.x
- 30. 如何禁止引用.NET DLL类库
我将此标记为公认的答案,因为它确实符合我的需求。 – 2011-04-21 03:42:00