Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6858871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:12:49+00:00 2026-05-27T02:12:49+00:00

I have come across a strange behavior with Microsoft Code Contracts that I don’t

  • 0

I have come across a strange behavior with Microsoft Code Contracts that I don’t understand and don’t know how to solve.

I am using Code Contracts in my projects, typically to check the preconditions (argument values), and have generally had no problems with it. However, in this one class, the Code Contracts module seems to automatically insert precondition checks on all methods that take arguments.

For example, this is my original code:

public override void WriteRaw(string data)
{
    this.writer.WriteRaw(data);
}

This is what I see when I decompile my assembly with Reflector:

public override void WriteRaw(string data)
{
    __ContractsRuntime.Requires(string.IsNullOrEmpty(data) == 0, null, "!string.IsNullOrEmpty(data)");
    this.writer.WriteRaw(data);
Label_0028:
    return;
}

Even after exhaustive research and analysis of my code, the best diagnosis I could come up with is that it happens only on the class that inherits/implements the abstract System.Xml.XmlWriter. In all other cases, the contracts will be there only if I add them manually.

In 99% of the cases I would welcome automatic argument checking by code contracts (provided that I can control it). But the code in question is a wrapping XML writer that is used by the XslCompilerTransform to omit proper XHTML. And if the XSL stylesheet contains something like <xsl:value-of select="xyz" disable-output-escaping="yes"/> where xyz doesn’t exist, because of the contract that was automatically generated the transform ends in an unnecessary exception that I cannot bypass unless I switch off contracts altogether.

Does anyone have some experience with this and can point me in the right direction?

Thanks,
Igor

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:12:50+00:00Added an answer on May 27, 2026 at 2:12 am

    The contracts are applied to the abstract XmlWriter interface. You can see this by looking at the contract reference assembly in the code contracts installation directory (on my machine it’s in C:\Program Files (x86)\Microsoft\Contracts\Contracts.NETFramework\v4.0) in Reflector.
    When you override a virtual/abstract method or interface method that has contracts applied, they are automatically woven into your code.

    The contract for WriteRaw is:

    public virtual void WriteRaw(string data)
    {
        Contract.Requires(!string.IsNullOrEmpty(data), null, "!string.IsNullOrEmpty(data)");
    }
    

    As far as I know, you can’t disable it without disabling contract checking.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have come across a strange behavior of Java that seems like a bug.
I have come across a strange bug in my code and I cannot understand
I have just come across something that is quite strange and yet I haven't
I have come across a strange defect. I am using Orbeon 3.9 CE on
I've come across some strange behavior trying to get files that start with a
I have come across some strange behavior in my iPhone Objective-C app. I am
I have come across Adobe MXML that looks very similar to Microsoft's XAML. Who
I have come across a very strange issue in python. (Using python 2.4.x) In
I have come across the following code that looks at divs contained inside divWiz
I have come across a strange behavior... In class A, at the viewDidLoad method

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.