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 6535035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:19:26+00:00 2026-05-25T10:19:26+00:00

I have some factory method public T Create<T> () where T : class {

  • 0

I have some factory method

    public T Create<T> () where T : class 
    {
        Contract.Ensures(Contract.Result<T>() != null);

        T result = this.unityContainer.Resolve<T>();

        return result;
    }

The I try to build the project i get the warning:

CodeContracts: ensures unproven: Contract.Result() != null

I understand that IUnityContainer interface does not have any contracts so Code Contracts think that varible may be null and there is no way to prove that Create() will return not null result.

How in this case I can make Code Contracts belive that result variable is not null?

I first tried to call Contract.Assert

    public T Create<T> () where T : class 
    {
        Contract.Ensures(Contract.Result<T>() != null);

        T result = this.unityContainer.Resolve<T>();

        Contract.Assert(result != null);

        return result;
    }

But it takes me another warning:

CodeContracts: assert unproven

I tried make check for null and this makes all warnings gone:

    public T Create<T> () where T : class 
    {
        Contract.Ensures(Contract.Result<T>() != null);

        T result = this.unityContainer.Resolve<T>();

        if (result == null)
        {
            throw new InvalidOperationException();
        }

        return result;
    }

But i’m not sure this is good solution to throw exception manually. May be there is some way to solve problem using Code Contracts only?

Thank you.

  • 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-25T10:19:26+00:00Added an answer on May 25, 2026 at 10:19 am

    I think you want Contract.Assume:

    Contract.Assume(result != null);
    

    From the docs:

    Instructs code analysis tools to assume that the specified condition is true, even if it cannot be statically proven to always be true.

    This will still validate the result at execution time if you have the rewriter appropriately configured.

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

Sidebar

Related Questions

I have some classes layed out like this class A { public virtual void
Consider I have a some Factory class: class Factory { public: Factory(); virtual ~Factory();
I have a generic object factory FactoryBase<T> with a factory method: public abstract class
I have some .NET remoting code where a factory method, implemented in some server
I have to implement a factory method pattern in C++. The class (C) that
I have some Abstract Factory public interface AbstractViewersFactory { IAbstractShapeViewer createRectangle(BaseOperationsListener<RectangleDTO> p); IAbstractShapeViewer createOval(BaseOperationsListener<OvalDTO>
I have some ASP.NET web services which all share a common helper class they
I have some code like this in a winforms app I was writing to
I have some kind of test data and want to create a unit test
I have a requirement to create some objects that implement a given interface, where

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.