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

  • SEARCH
  • Home
  • 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 8593725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:03:22+00:00 2026-06-12T00:03:22+00:00

I have a code contract expresses as this – it validates that entity to

  • 0

I have a code contract expresses as this – it validates that entity to be stored is not null and is valid for persistence. It works. Fab.

[ContractClassFor(typeof(IRepository<,>))]
internal abstract class ContractsForIRepository<T, TId> : IRepository<T, TId> where T : IEntity
{
    private ContractsForIRepository()
    {

    } 

    public T Persist(T entity)
    {
        Contract.Requires<InvalidEntityException>(entity != null, "Entity is null");
        Contract.Requires<InvalidEntityException>(entity.IsValidForPersistence(), "Entity not valid for persistence");
        return default(T);
    }

}

However, I would like the exception to be more useful – as anyone receiving the message will want to know what entity was invalid and what is looked like. All of the entities override ToString(), so I wanted to include this in the error message:

Contract.Requires<InvalidEntityException>(entity.IsValidForPersistence(), "Entity not valid for persistence " + entity.ToString());

I have included ToString to be explicit – it would be called implicitly if I omitted it, but I think it makes my question clearer.

The problem is, this isn’t allowed by code contracts and I get the following message.

User message to contract call can only be string literal, or a static field, or static property that is at least internally visible.

Is there any way to include specific data in the exception message?

  • 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-06-12T00:03:23+00:00Added an answer on June 12, 2026 at 12:03 am

    According the documentation:

    2.10 Overloads on Contract Methods

    All of the contract methods have overloads that take a string in
    addition to the boolean condition:

    Contract.Requires(x != null, "If x is null, then the missiles are red!");

    The user-supplied string will be displayed whenever the contract is
    violated at runtime. Currently, it must be a compile-time constant.

    So, what you are asking is not possible.

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

Sidebar

Related Questions

I have this simple code: public ArrayStack(int capacity) { Contract.Requires(capacity >= 0); Contract.Ensures(_items !=
I have code that looks something like this: public class Foo<T> : ObservableCollection<T> {
I have code that generates a List<string[]> variable but can't quite figure out how
I have code like this in my view model: function ChatListViewModel(chats) { var self
I have code that looks more or less like the code below but it
Okay, I have yet another Code Contracts question. I have a contract on an
I have this code in my WCF Service: public class MyImage { public Image
I have this code structure: public abstract class ContentEntryBase { public string UniqueIdentifier; public
I have a piece of code for which cccheck tells me that a Requires()
I have a SOAP web service that is defined contract-first--the request and response xml

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.