Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.NETGuard类库?_.net_Code Contracts - Fatal编程技术网

.NETGuard类库?

.NETGuard类库?,.net,code-contracts,.net,Code Contracts,我正在寻找一个库或源代码,它提供诸如检查空参数之类的保护方法。显然,这是一个相当简单的构建,但我想知道是否已经有了.NET。基本的谷歌搜索没有透露太多信息。鉴于微软推出了.NET 4.0,如果可能的话,我会尝试找到一个最兼容的,如果不兼容,请自己编写。这样,当您升级到.NET 4.0(最终)时,迁移将更加容易。有几种方法可以使用 我最喜欢的是使用面向方面编程。查看PostSharp 您还可以查看Spec#,它是C语言的一个扩展# 在4.0中,您将拥有一个功能齐全的合同库 最后,我的一位同事提出了

我正在寻找一个库或源代码,它提供诸如检查空参数之类的保护方法。显然,这是一个相当简单的构建,但我想知道是否已经有了.NET。基本的谷歌搜索没有透露太多信息。

鉴于微软推出了.NET 4.0,如果可能的话,我会尝试找到一个最兼容的,如果不兼容,请自己编写。这样,当您升级到.NET 4.0(最终)时,迁移将更加容易。

有几种方法可以使用

我最喜欢的是使用面向方面编程。查看PostSharp

您还可以查看Spec#,它是C语言的一个扩展#

在4.0中,您将拥有一个功能齐全的合同库

最后,我的一位同事提出了一个非常有用的警卫库:

我不知道有什么商品可以买到。模式与实践企业库中对这种类型的代码有一些支持。在CodePlex上也有很多开源项目似乎也在做这件事(程度不同)

大多数情况下,这些类型的库最终都是定制编写的,并且是使用它们的公司内部的


.NET 4.0支持使用基于规范的机制来实现这一点。

我最近写了一篇关于guard类的文章(也没有找到任何信息):

我还发布了相应的Guard类实现(可以按原样使用此代码,也可以根据您的需要进行调整):ajdotnet.wordpress.com/Guard-class/

关于.NET 4.0(规范#的后续版本)中保护类和代码契约之间的关系,请参阅以下文章:www.leading-edge-dev.de/?p=438

(对于碎片链接,很抱歉,该站点只允许一个链接…)

嗨, AJ.NET

有。页面中的用法示例:

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;
}
公共ICollection GetData(可为空的id、字符串xml、ICollection列) { //检查所有先决条件: 身份证(“身份证”) .IsNotNull()//失败时引发ArgumentNullException .IsInRange(1999)//失败时ArgumentOutOfRange异常 .IsNotEqualTo(128);//失败时抛出ArgumentException 需要(“xml”) .StartsWith(“”//失败时引发ArgumentException .EndsWith(“”;//失败时引发ArgumentException col.Requires(“col”) .IsNotNull()//失败时引发ArgumentNullException .IsEmpty();//失败时抛出ArgumentException //做些工作 //示例:调用不应返回null的方法 对象结果=构建结果(xml,col); //检查所有后置条件: 结果。确保(“结果”) .IsOfType(typeof(ICollection));//失败时抛出后条件异常 返回(ICollection)结果; } 另一种很好的方法,虽然不打包在库中,但很容易做到:

publicstaticvoidcopy(T[]dst,longdstoffset,T[]src,longdsroffset,long-length)
{
Validate.Begin()
.IsNotNull(dst,“dst”)
.IsNotNull(src,“src”)
.检查()
.IsPositive(长度)
.ISindexRange(dst、dstOffset、“dstOffset”)
.IsIndexRange(dst、dst偏移量+长度,“dst偏移量+长度”)
.IsIndexRange(src,srcOffset,“srcOffset”)
.IsIndexRange(src、srcOffset+长度,“srcOffset+长度”)
.检查();
对于(int di=dstOffset;di

我在中使用它,您可以从那里借用代码。

安装netfx guard nuget软件包。您还可以获得notnull和notempty代码段,它的执行速度与手动检查的速度一样快
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];
}