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


C# Class.GetConstructors方法代码示例

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


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

示例1: ImmutabilityChecksOnClasses

    private void ImmutabilityChecksOnClasses(Class Class) {
      if (Class == null) return;
      TypeNode baseType = Class.BaseType;

      bool classIsImmutable = Class.GetAttribute(SystemTypes.ImmutableAttribute) != null;
      String ClassName = Class.Name.ToString();

      if (baseType != null) { // otherwise, there are other errors.
        // A class _can_ be declared [Immutable] iff its superclass is immutable or if its superclass is System.Object.
        if (classIsImmutable &&
            baseType != SystemTypes.Object &&
            baseType.GetAttribute(SystemTypes.ImmutableAttribute) == null)
          this.HandleError(Class.Name, Error.ImmutableHasMutableBase, ClassName, baseType.Name.ToString());

        // A class _must_ be declared [Immutable] if its superclass is immutable (except if superclass is Object)
        if (!classIsImmutable &&
            baseType != SystemTypes.Object &&
            baseType.GetAttribute(SystemTypes.ImmutableAttribute) != null)
          this.HandleError(Class.Name, Error.MutableHasImmutableBase, ClassName, baseType.Name.ToString());
      }

      foreach (Member mem in Class.Members) {
        Field f = mem as Field;
        if (f == null) continue;

        // An immutable class cannot have peer fields
        if (classIsImmutable && f.IsPeer)
          this.HandleError(mem.Name, Error.ImmutableClassHasPeerField, ClassName, mem.ToString());

        // Peer fields and rep fields cannot be of immutable type
        if ((f.IsOwned || f.IsPeer) && f.Type.GetAttribute(SystemTypes.ImmutableAttribute) != null)
          this.HandleError(mem.Name, Error.RepOrPeerFieldImmutable, ClassName, mem.ToString());
      }

      // In constructors of immutable classes, base() must be called as the very last statement
      if (classIsImmutable) {
        MemberList constructors = Class.GetConstructors();
        foreach (Member constructor in constructors) {
          if (constructor is InstanceInitializer && ((InstanceInitializer)constructor).IsCompilerGenerated) continue;
          if (constructor.GetAttribute(ExtendedRuntimeTypes.NotDelayedAttribute) == null) continue;
          StatementList stmts = ((Method)constructor).Body.Statements;
          if (stmts == null) { continue; }
          Statement lastStmt = stmts[stmts.Count - 1];
          if (lastStmt is Block) {
            stmts = ((Block)lastStmt).Statements;
            lastStmt = stmts[stmts.Count - 1];
          }
          // last stmt must be base() or this()
          bool error = false;
          if (!(lastStmt is ExpressionStatement)) error = true;            
          else {
            Expression exp = ((ExpressionStatement)lastStmt).Expression;
            if (!(exp is MethodCall)) error = true;              
            else {
              Expression callee = ((MethodCall)exp).Callee;
              if (callee == null || !(callee is MemberBinding)) error = true;                
              else {
                Member mb = ((MemberBinding)callee).BoundMember;
                if (mb == null || !(mb is InstanceInitializer) || 
                    !(Class.BaseClass == ((InstanceInitializer)mb).DeclaringType ||
                      Class == ((InstanceInitializer)mb).DeclaringType)) {
                  error = true;
                }
              }
            }
          }
          if (error)
            this.HandleError(constructor.Name, Error.ImmutableConstructorLastStmtMustBeBaseOrThis, ClassName);
          // other stmts may not be base
          for (int i = 0; i < stmts.Count - 1; i++) {
            if (!(stmts[i] is ExpressionStatement)) continue;
            Expression exp = ((ExpressionStatement)stmts[i]).Expression;
            if (!(exp is MethodCall)) continue;
            Expression callee = ((MethodCall)exp).Callee;
            if (callee == null || !(callee is MemberBinding)) continue;
            Member mb = ((MemberBinding)callee).BoundMember;
            if (mb != null && mb is InstanceInitializer && Class.BaseClass == ((InstanceInitializer)mb).DeclaringType) {
              this.HandleError(constructor.Name, Error.ImmutableConstructorBaseNotLast, ClassName);
              break;
            }
          }
        }
      }

      // An immutable interface can only be implemented by an immutable class
      foreach (Interface iface in Class.Interfaces) {
        if (iface == null) continue;
        if (iface.GetAttribute(SystemTypes.ImmutableAttribute) != null && !classIsImmutable)
          this.HandleError(Class.Name, Error.ImmutableIfaceImplementedByMutableClass, ClassName, iface.Name.ToString());
      }
    }
开发者ID:hesam,项目名称:SketchSharp,代码行数:91,代码来源:Checker.cs


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