C#中下限非零的数组解析(2)

看到以上的代码,应该对非零基数组的创建有一个大致了解,接下来具体看一下Ensures()方法的底层代码:
1234 public static void Ensures(bool condition)
        {
            AssertMustUseRewriter(ContractFailureKind.Postcondition, "Ensures"); 
        }

1234567891011121314151617181920212223242526 [SecuritySafeCritical]
        static partial void AssertMustUseRewriter(ContractFailureKind kind, String contractKind) 
        {
            if (_assertingMustUseRewriter) 
                System.Diagnostics.Assert.Fail("Asserting that we must use the rewriter went reentrant.", "Didn't rewrite this mscorlib?"); 
            _assertingMustUseRewriter = true;
            Assembly thisAssembly = typeof(Contract).Assembly; 
            StackTrace stack = new StackTrace(); 
            Assembly probablyNotRewritten = null;
            for (int i = 0; i < stack.FrameCount; i++) 
            { 
                Assembly caller = stack.GetFrame(i).GetMethod().DeclaringType.Assembly;
                if (caller != thisAssembly) 
                {
                    probablyNotRewritten = caller;
                    break;
                } 
            }
 
            if (probablyNotRewritten == null) 
                probablyNotRewritten = thisAssembly;
            String simpleName = probablyNotRewritten.GetName().Name; 
            System.Runtime.CompilerServices.ContractHelper.TriggerFailure(kind, Environment.GetResourceString("MustUseCCRewrite", contractKind, simpleName), null, null, null);
 
            _assertingMustUseRewriter = false;
        }

有关非零基数组的创建方法就不做深入的介绍,如果需要使用,可以根据提供的方法重载选择对应的版本实现。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/ba36f9b90bdbc894d89c2dc690a2d479.html