与接口的代码约定:"跳过方法调用.编译器将生成方法调用,因为该方法是有条件的...... [...]"

Jör*_*ann 9 .net code-contracts

晚上好,

我刚开始玩Microsoft.Contracts(最新版本)并将其插入到示例界面之上,现在它看起来像这样:

namespace iRMA2.Core.Interfaces
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.Composition;
    using System.Diagnostics.Contracts;

    /// <summary>
    /// Base Interface declarations for iRMA2 Extensions
    /// </summary>
    [InheritedExport]
    [ContractClass(typeof(IiRMA2ExtensionContract))]
    public interface IiRMA2Extension
    {
        /// <summary>
        /// Gets the name.
        /// </summary>
        /// <value>The name of the Extension.</value>
        string Name { get; }

        /// <summary>
        /// Gets the description.
        /// </summary>
        /// <value>The description.</value>
        string Description { get; }

        /// <summary>
        /// Gets the author of the extension. Please provide complete information to get in touch with author(s) and the corresponding department
        /// </summary>
        /// <value>The author of the extensions.</value>
        string Author { get; }

        /// <summary>
        /// Gets the major version.
        /// </summary>
        /// <value>The major version of the extension.</value>
        int MajorVersion { get; }

        /// <summary>
        /// Gets the minor version.
        /// </summary>
        /// <value>The minor version.</value>
        int MinorVersion { get; }

        /// <summary>
        /// Gets the build number.
        /// </summary>
        /// <value>The build number.</value>
        int BuildNumber { get; }

        /// <summary>
        /// Gets the revision.
        /// </summary>
        /// <value>The revision.</value>
        int Revision { get; }

        /// <summary>
        /// Gets the depends on.
        /// </summary>
        /// <value>The dependencies to other <c>IiRMA2Extension</c> this one has.</value>
        IList<IiRMA2Extension> DependsOn { get; }
    }

    /// <summary>
    /// Contract class for <c>IiRMA2Extension</c>
    /// </summary>
    [ContractClassFor(typeof(IiRMA2Extension))]
    internal sealed class IiRMA2ExtensionContract : IiRMA2Extension
    {
        #region Implementation of IiRMA2Extension

        /// <summary>
        /// Gets or sets the name.
        /// </summary>
        /// <value>The name of the Extension.</value>
        public string Name
        {
            get
            {
                Contract.Ensures(!String.IsNullOrEmpty(Contract.Result<string>()));
                return default(string);
            }

            set
            {
                Contract.Requires(value != null);
            }
        }

        /// <summary>
        /// Gets the description.
        /// </summary>
        /// <value>The description.</value>
        public string Description
        {
            get { throw new NotImplementedException(); }
        }

        /// <summary>
        /// Gets the author of the extension. Please provide complete information to get in touch with author(s) and the corresponding department
        /// </summary>
        /// <value>The author of the extensions.</value>
        public string Author
        {
            get { throw new NotImplementedException(); }
        }

        /// <summary>
        /// Gets the major version.
        /// </summary>
        /// <value>The major version of the extension.</value>
        public int MajorVersion
        {
            get { throw new NotImplementedException(); }
        }

        /// <summary>
        /// Gets the minor version.
        /// </summary>
        /// <value>The minor version.</value>
        public int MinorVersion
        {
            get { throw new NotImplementedException(); }
        }

        /// <summary>
        /// Gets the build number.
        /// </summary>
        /// <value>The build number.</value>
        public int BuildNumber
        {
            get { throw new NotImplementedException(); }
        }

        /// <summary>
        /// Gets the revision.
        /// </summary>
        /// <value>The revision.</value>
        public int Revision
        {
            get { throw new NotImplementedException(); }
        }

        /// <summary>
        /// Gets the Extensions this one depends on.
        /// </summary>
        /// <value>The dependencies to other <c>IiRMA2Extension</c> this one has.</value>
        public IList<IiRMA2Extension> DependsOn
        {
            get
            {
                Contract.Ensures(Contract.Result<IList<IiRMA2Extension>>() != null);
                return default(IList<IiRMA2Extension>);
            }
        }

        #endregion
    }
}
Run Code Online (Sandbox Code Playgroud)

现在为什么两个Contract.Ensures(...)在视觉上"模糊"了工具提示" 跳过方法调用.编译器将生成方法调用,因为该方法是有条件的,或者是没有实现的部分方法 ",实际上是CodeContracts输出不计算/显示它们...我在这里错过了什么,做错了什么?

-J

Jar*_*Par 12

您是否为此版本定义了适当的代码合约宏?比如CONTRACTS_FULL?没有定义正确的宏可能导致方法被排除在编译之外.

  • @JörgB.:我想你正在使用Resharper.Resharper不知道CONTRACTS_FULL将在编译时定义,除非您将其放入项目的"条件编译符号"中.这应该解决它...但它的缺点是,如果你改变你正在使用的合同级别,你需要改变*和*代码合同页面. (4认同)
  • @Porges:是的,是的.另外,我在http://youtrack.jetbrains.net/issue/RSRP-182553上为r#创建了一个错误报告,以便为R#支持代码合同,因为它代码重构建议/警告. (2认同)
  • 将csproj文件中的<DefineConstants> DEBUG; TRACE </ DefineConstants>更改为<DefineConstants> DEBUG; TRACE; CONTRACTS_FULL </ DefineConstants>已经摆脱了r#问题. (2认同)