如何写出这个特殊的前提条件? [英] How to write this special precondition?

查看:88
本文介绍了如何写出这个特殊的前提条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



是否可以使用代码合约重写以下参数检查?

Hi,

is it possible to rewrite the following argument check with Code Contracts?


      public static ConstructorInvocation<TResult> CreateConstructorInvocation<TResult>(Type type) {
         ConstructorInfo constructor = type.GetConstructor(
            bindingAttr: BindingFlags.Public | BindingFlags.Instance,
            binder: null,
            types: Type.EmptyTypes,
            modifiers: null
         );

         if (constructor == null) {
            throw new ArgumentException(
               "Failed to create a new instance of type '" + type.Name + "'. The " +
               "type does not define a public parameterless constructor."
            );
         }

         // Some code that works with constructor
         return null;
      }

推荐答案



第二种变体是官方支持的。唯一的要求是HasConstructor必须标有[Pure]属性。我不能说第一个变种。
Hi,

The second variant is officially supported. The only requirement is that HasConstructor must be marked with [Pure] attribute. I cannot say anything about first variant.


这篇关于如何写出这个特殊的前提条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆