Microsoft.JScript.Class.GetTypeBuilderOrEnumBuilder C# (CSharp) Method

GetTypeBuilderOrEnumBuilder() private method

private GetTypeBuilderOrEnumBuilder ( ) : Type
return System.Type
      internal virtual Type GetTypeBuilderOrEnumBuilder(){
        if (this.classob.classwriter != null) return this.classob.classwriter;
        if (!this.isAlreadyPartiallyEvaluated) this.PartiallyEvaluate();
        Type superType = null;
        if (this.superTypeExpression != null)
          superType = this.superTypeExpression.ToType();
        else
          superType = this.isInterface ? null : Typeob.Object;
        int offset = (this.needsEngine ? 1 : 0) + (this.generateCodeForExpando ? 1 : 0);
        int n = this.interfaces.Length+offset;
        Type[] interfaces = new Type[n];
        for (int i = offset; i < n; i++)
          interfaces[i] = this.interfaces[i-offset].ToType();
        if (this.needsEngine)
          interfaces[--offset] = Typeob.INeedEngine;
        if (this.generateCodeForExpando)
          interfaces[--offset] = Typeob.IEnumerable;
        
        TypeBuilder result;
        if (this.enclosingScope is ClassScope){
          if ((result = (TypeBuilder)this.classob.classwriter) == null){
            TypeBuilder enclosingClass = ((ClassScope)this.enclosingScope).owner.GetTypeBuilder();
            if (this.classob.classwriter != null) return this.classob.classwriter;
            result = enclosingClass.DefineNestedType(this.name, this.attributes, superType, interfaces);
            this.classob.classwriter = result;
            if (!this.isStatic && !this.isInterface)
              this.classob.outerClassField = result.DefineField("outer class instance", enclosingClass, FieldAttributes.Private);
          }
        }else{
          String scopeName = ((ActivationObject)this.enclosingScope).GetName();
          if (scopeName == null){
            VsaEngine engine = this.context.document.engine;
            if (engine != null && engine.genStartupClass)
              scopeName = engine.RootNamespace;
          }
          if ((result = (TypeBuilder)this.classob.classwriter) == null){
            string typeName = this.name;
            if (scopeName != null)
              typeName = scopeName+"."+typeName;
            if (typeName.Length >= 1024) {
              this.context.HandleError(JSError.TypeNameTooLong, typeName);
              typeName = "bad type name "+badTypeNameCount.ToString(CultureInfo.InvariantCulture);
              badTypeNameCount++;
            }
            result = compilerGlobals.module.DefineType(typeName, this.attributes, superType, interfaces);
            this.classob.classwriter = result;
          }
        }
        // deal with custom attributes
        if (this.customAttributes != null){
          CustomAttributeBuilder[] custAtt = this.customAttributes.GetCustomAttributeBuilders(false);
          for (int j = 0; j < custAtt.Length; j++){
            result.SetCustomAttribute(custAtt[j]);
          }
        }
        if (this.clsCompliance == CLSComplianceSpec.CLSCompliant)
          result.SetCustomAttribute(new CustomAttributeBuilder(CompilerGlobals.clsCompliantAttributeCtor, new Object[]{true}));
        else if (this.clsCompliance == CLSComplianceSpec.NonCLSCompliant)
          result.SetCustomAttribute(new CustomAttributeBuilder(CompilerGlobals.clsCompliantAttributeCtor, new Object[]{false}));
        if (this.generateCodeForExpando)
          result.SetCustomAttribute(new CustomAttributeBuilder(CompilerGlobals.defaultMemberAttributeCtor, new Object[]{"Item"}));

        //Define the class fields        
        for (int i = 0, m = this.fields.Length; i < m; i++){
          JSMemberField field = this.fields[i];
          if (field.IsLiteral){
            Object value = field.value;
            if (value is JSProperty){
              JSProperty prop = (JSProperty)value;
              ParameterInfo[] pars = prop.GetIndexParameters();
              int np = pars.Length;
              Type[] ptypes = new Type[np];
              for (int j = 0; j < np; j++)
                ptypes[j] = pars[j].ParameterType;
              PropertyBuilder pb = prop.metaData = result.DefineProperty(field.Name, prop.Attributes, prop.PropertyType, ptypes);
              if (prop.getter != null){
                CustomAttributeList cal = ((JSFieldMethod)prop.getter).func.customAttributes;
                if (cal != null){
                  CustomAttributeBuilder[] custAttrs = cal.GetCustomAttributeBuilders(true);
                  foreach (CustomAttributeBuilder cb in custAttrs) pb.SetCustomAttribute(cb);
                }
                pb.SetGetMethod((MethodBuilder)prop.getter.GetMethodInfo(compilerGlobals));
              }
              if (prop.setter != null){
                CustomAttributeList cal = ((JSFieldMethod)prop.setter).func.customAttributes;
                if (cal != null){
                  CustomAttributeBuilder[] custAttrs = cal.GetCustomAttributeBuilders(true);
                  foreach (CustomAttributeBuilder cb in custAttrs) pb.SetCustomAttribute(cb);
                }
                pb.SetSetMethod((MethodBuilder)prop.setter.GetMethodInfo(compilerGlobals));
              }
              continue;
            }else if (value is ClassScope){
              ((ClassScope)value).GetTypeBuilderOrEnumBuilder();
            }else if (Convert.GetTypeCode(value) != TypeCode.Object){
              FieldBuilder fb = result.DefineField(field.Name, field.FieldType, field.Attributes); 
              fb.SetConstant(field.value);
              field.metaData = fb;
              field.WriteCustomAttribute(this.Engine.doCRS);
              continue;
            }else if (value is FunctionObject){
              FunctionObject func = (FunctionObject)value;
              if (func.isExpandoMethod){
                field.metaData = result.DefineField(field.Name, Typeob.ScriptFunction, field.Attributes&~(FieldAttributes.Literal|FieldAttributes.Static));
                func.isStatic = false;
              }
              if (this.isInterface)
                do{
                  func.GetMethodInfo(compilerGlobals);
                  field = field.nextOverload;
                  if (field == null) break;
                  func = (FunctionObject)field.value;
                }while(true);
            }
            continue;
          }
          field.metaData = result.DefineField(field.Name, field.FieldType, field.Attributes);
          field.WriteCustomAttribute(this.Engine.doCRS);
        }
        
        return result;
      }