当前位置: 首页>>代码示例>>C#>>正文


C# Method.GetAttributeFromSelfOrDeclaringMember方法代码示例

本文整理汇总了C#中System.Compiler.Method.GetAttributeFromSelfOrDeclaringMember方法的典型用法代码示例。如果您正苦于以下问题:C# Method.GetAttributeFromSelfOrDeclaringMember方法的具体用法?C# Method.GetAttributeFromSelfOrDeclaringMember怎么用?C# Method.GetAttributeFromSelfOrDeclaringMember使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Compiler.Method的用法示例。


在下文中一共展示了Method.GetAttributeFromSelfOrDeclaringMember方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: VisitMethod


//.........这里部分代码省略.........
      #endregion
      #region Can't have both [Rep] and [Peer]
      bool isRep = method.GetAttribute(SystemTypes.RepAttribute) != null;
      bool isPeer = method.GetAttribute(SystemTypes.PeerAttribute) != null;
      if (isRep && isPeer) {
        this.HandleError(method, Error.ConflictingAttributes, this.GetTypeName(SystemTypes.RepAttribute),
          this.GetTypeName(SystemTypes.PeerAttribute));
      }
      #endregion

      #region NoReferenceComparison
      attrNode = method.GetAttribute(SystemTypes.NoReferenceComparisonAttribute);
      overridenAttr = null;
      if (overriden != null)
        overridenAttr = overriden.GetAttribute(SystemTypes.NoReferenceComparisonAttribute);
      if (attrNode == null && overridenAttr != null)
        this.HandleError(method, Error.NoReferenceComparisonAttrNotCopied, method.Name.ToString());
      else if (attrNode != null && !(method.IsPure || method.IsConfined || method.IsStateIndependent))
        this.HandleError(method, Error.NonPureMarkedNoReferenceComparison);
      else if (attrNode != null) {
        NoReferenceComparisonVisitor visitor = new NoReferenceComparisonVisitor(this);
        this.TransferStateTo(visitor);
        visitor.Visit(method.Body);
        if (!visitor.HasNoReferenceComparison)
          this.HandleError(method, Error.ViolatedNoReferenceComparison, method.Name.ToString());
      }
      #endregion
      #region ResultNotNewlyAllocated
      attrNode = method.GetAttribute(SystemTypes.ResultNotNewlyAllocatedAttribute);
      overridenAttr = null;
      if (overriden != null)
        overridenAttr = overriden.GetAttribute(SystemTypes.ResultNotNewlyAllocatedAttribute);
      if (attrNode == null && overridenAttr != null)
        this.HandleError(method, Error.ResultNotNewlyAllocatedAttrNotCopied, method.Name.ToString());
      else if (attrNode != null && !(method.IsPure || method.IsConfined || method.IsStateIndependent))
        this.HandleError(method, Error.NonPureMarkedResultNotNewlyAllocated);
      else if (attrNode != null && (method.ReturnType == null || method.ReturnType.IsValueType))
        this.HandleError(method, Error.NonRefTypeMethodMarkedResultNotNewlyAllocated);
      #endregion
      #region Pure methods cannot have ref parameters
      if (!method.IsPropertyGetter && (method.IsPure || method.IsConfined || method.IsStateIndependent)) {
        // don't check property getters: they already will have an error if they have out or ref parameters.
        for (int i = 0, n = method.Parameters == null ? 0 : method.Parameters.Count; i < n; i++) {
          Parameter p = method.Parameters[i];
          if (p.IsOut) continue;
          Reference r = p.Type as Reference;
          if (r != null) {
            this.HandleError(p, Error.PureMethodCannotHaveRefParam);
          }
        }
      }
      #endregion
      #region Pure methods cannot have modifies clauses
      if ((method.IsPure || method.IsConfined || method.IsStateIndependent) && method.Contract != null && method.Contract.Modifies != null && 0 < method.Contract.Modifies.Count) {
        this.HandleError(method, Error.PureMethodCannotHaveModifies);
      }
      #endregion Pure methods cannot have modifies clauses

      bool b;
      TypeNode returnType = method.ReturnType;
      if (returnType != null)
        returnType = returnType.StripOptionalModifiers(out b);
      if ((method.GetAttribute(SystemTypes.RepAttribute) != null || method.GetAttribute(SystemTypes.PeerAttribute) != null) && 
          !(returnType.IsReferenceType || (method.IsPropertyGetter && ((Property)method.DeclaringMember).Type.IsReferenceType)))
        this.HandleError(method, Error.BadUseOfOwnedOnMethod);
      else if (method.IsVirtual && !method.IsPropertyGetter && MemberIsRep(method)) // note: second conjunct mainly to get Boogie through
        this.HandleError(method, Error.UseOfRepOnVirtualMethod);

      #region Make sure purity attributes are used correctly
      AttributeNode pureAttr = method.GetAttributeFromSelfOrDeclaringMember(SystemTypes.PureAttribute);
      AttributeNode readsAttr = method.GetAttributeFromSelfOrDeclaringMember(SystemTypes.ReadsAttribute);
      if (readsAttr != null) {
        if (pureAttr == null) {
          this.HandleError(method, Error.ReadsWithoutPure);
        }
        Literal l = readsAttr.GetPositionalArgument(0) as Literal;
        // default ctor for Reads sets it to Owned
        Microsoft.Contracts.ReadsAttribute.Reads r =
          l == null ?
          Microsoft.Contracts.ReadsAttribute.Reads.Owned :
          (Microsoft.Contracts.ReadsAttribute.Reads)l.Value;
        switch (r) {
          case Microsoft.Contracts.ReadsAttribute.Reads.Everything:
            if (!method.IsPure)
              this.HandleError(method, Error.InconsistentPurityAttributes);
            break;
          case Microsoft.Contracts.ReadsAttribute.Reads.Nothing:
            if (!method.IsStateIndependent)
              this.HandleError(method, Error.InconsistentPurityAttributes);
            break;
          case Microsoft.Contracts.ReadsAttribute.Reads.Owned:
            if (!method.IsConfined)
              this.HandleError(method, Error.PureOwnedNotAllowed);
            break;
        }
      }
      #endregion Make sure purity attributes are used correctly

      return result;
    }
开发者ID:hesam,项目名称:SketchSharp,代码行数:101,代码来源:Checker.cs

示例2: GetPurityAttribute

 private AttributeNode GetPurityAttribute(Method method) {
   if (method == null || method.Attributes == null) return null;
   return method.GetAttributeFromSelfOrDeclaringMember(SystemTypes.PureAttribute);
 }
开发者ID:hesam,项目名称:SketchSharp,代码行数:4,代码来源:Checker.cs


注:本文中的System.Compiler.Method.GetAttributeFromSelfOrDeclaringMember方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。