protected virtual bool WriteObject(string objectName, List <TypeConfigItem> members, string format, string interfaceFormat) { bool hasProperties = HasProperties(objectName, members); int pos = 0; IWriterInfo writer = null; bool beginBlock = false; if (hasProperties && objectName != null && !IsObjectLiteral) { beginBlock = true; pos = Emitter.Output.Length; writer = SaveWriter(); EnsureComma(); Write(objectName); WriteColon(); BeginBlock(); } bool isProperty = JS.Fields.PROPERTIES == objectName; bool isField = JS.Fields.FIELDS == objectName; int count = 0; foreach (var member in members) { object constValue = null; bool isPrimitive = false; bool write = false; bool writeScript = false; if (member.Initializer is PrimitiveExpression primitiveExpr) { //isPrimitive = true; constValue = primitiveExpr.Value; ResolveResult rr = null; if (member.VarInitializer != null) { rr = Emitter.Resolver.ResolveNode(member.VarInitializer); } else { rr = Emitter.Resolver.ResolveNode(member.Entity); } if (rr != null && rr.Type.Kind == TypeKind.Enum) { constValue = Helpers.GetEnumValue(Emitter, rr.Type, constValue); writeScript = true; } } if (constValue is RawValue) { constValue = constValue.ToString(); write = true; writeScript = false; } var isNull = member.Initializer.IsNull || member.Initializer is NullReferenceExpression || member.Initializer.Parent == null; if (!isNull && !isPrimitive) { var constrr = Emitter.Resolver.ResolveNode(member.Initializer); if (constrr != null && constrr.IsCompileTimeConstant) { //isPrimitive = true; constValue = constrr.ConstantValue; var expectedType = Emitter.Resolver.Resolver.GetExpectedType(member.Initializer); if (!expectedType.Equals(constrr.Type) && expectedType.Kind != TypeKind.Dynamic) { try { constValue = Convert.ChangeType(constValue, ReflectionHelper.GetTypeCode(expectedType)); } catch (Exception) { Logger.ZLogWarning("FieldBlock: Convert.ChangeType is failed. Value type: {0}, Target type: {1}", constrr.Type.FullName, expectedType.FullName); } } if (constrr.Type.Kind == TypeKind.Enum) { constValue = Helpers.GetEnumValue(Emitter, constrr.Type, constrr.ConstantValue); } writeScript = true; } } var isNullable = false; if (isPrimitive && constValue is AstType) { var itype = Emitter.Resolver.ResolveNode((AstType)constValue); if (NullableType.IsNullable(itype.Type)) { isNullable = true; } } string tpl = null; IMember templateMember = null; MemberResolveResult init_rr = null; if (isField && member.VarInitializer != null) { init_rr = Emitter.Resolver.ResolveNode(member.VarInitializer) as MemberResolveResult; tpl = init_rr != null?Emitter.GetInline(init_rr.Member) : null; if (tpl != null) { templateMember = init_rr.Member; } } bool isAutoProperty = false; if (isProperty) { var member_rr = Emitter.Resolver.ResolveNode(member.Entity) as MemberResolveResult; var property = (IProperty)member_rr.Member; isAutoProperty = Helpers.IsAutoProperty(property); } bool written = false; if (!isNull && (!isPrimitive || constValue is AstType || tpl != null) && !(isProperty && !IsObjectLiteral && !isAutoProperty)) { string value = null; bool needContinue = false; string defValue = ""; if (!isPrimitive) { var oldWriter = SaveWriter(); NewWriter(); member.Initializer.AcceptVisitor(Emitter); value = Emitter.Output.ToString(); RestoreWriter(oldWriter); ResolveResult rr = null; AstType astType = null; if (member.VarInitializer != null) { rr = Emitter.Resolver.ResolveNode(member.VarInitializer); } else { astType = member.Entity.ReturnType; rr = Emitter.Resolver.ResolveNode(member.Entity); } constValue = Inspector.GetDefaultFieldValue(rr.Type, astType); if (rr.Type.Kind == TypeKind.Enum) { constValue = Helpers.GetEnumValue(Emitter, rr.Type, constValue); } isNullable = NullableType.IsNullable(rr.Type); needContinue = constValue is IType; writeScript = true; /*if (needContinue && !(member.Initializer is ObjectCreateExpression)) * { * defValue = " || " + Inspector.GetStructDefaultValue((IType)constValue, this.Emitter); * }*/ } else if (constValue is AstType) { value = isNullable ? "null" : Inspector.GetStructDefaultValue((AstType)constValue, Emitter); constValue = value; write = true; needContinue = !isProperty && !isNullable; } var name = member.GetName(Emitter); bool isValidIdentifier = Helpers.IsValidIdentifier(name); if (isProperty && isPrimitive) { constValue = "null"; if (IsObjectLiteral) { written = true; if (isValidIdentifier) { Write(string.Format("this.{0} = {1};", name, value)); } else { Write(string.Format("this[{0}] = {1};", ToJavaScript(name, Emitter), value)); } WriteNewLine(); } else { Injectors.Add(string.Format(name.StartsWith("\"") || !isValidIdentifier ? "this[{0}] = {1};" : "this.{0} = {1};", isValidIdentifier ? name : ToJavaScript(name, Emitter), value)); } } else { if (IsObjectLiteral) { written = true; if (isValidIdentifier) { Write(string.Format("this.{0} = {1};", name, value + defValue)); } else { Write(string.Format("this[{0}] = {1};", ToJavaScript(name, Emitter), value + defValue)); } WriteNewLine(); } else if (tpl != null) { if (!tpl.Contains("{0}")) { tpl = tpl + " = {0};"; } string v = null; if (!isNull && (!isPrimitive || constValue is AstType)) { v = value + defValue; } else { if (write) { v = constValue != null?constValue.ToString() : ""; } else if (writeScript) { v = ToJavaScript(constValue, Emitter); } else { var oldWriter = SaveWriter(); NewWriter(); member.Initializer.AcceptVisitor(Emitter); v = Emitter.Output.ToString(); RestoreWriter(oldWriter); } } tpl = Helpers.ConvertTokens(Emitter, tpl, templateMember); tpl = tpl.Replace("{this}", "this").Replace("{0}", v); if (!tpl.EndsWith(";")) { tpl += ";"; } Injectors.Add(tpl); } else { bool isDefaultInstance = Emitter.Resolver.ResolveNode(member.Initializer) is CSharpInvocationResolveResult rr && rr.Member.SymbolKind == SymbolKind.Constructor && rr.Arguments.Count == 0 && rr.InitializerStatements.Count == 0 && rr.Type.Kind == TypeKind.Struct; if (!isDefaultInstance) { if (isField && !isValidIdentifier) { Injectors.Add(string.Format("this[{0}] = {1};", name.StartsWith("\"") ? name : ToJavaScript(name, Emitter), value + defValue)); } else { Injectors.Add(string.Format(name.StartsWith("\"") ? interfaceFormat : format, name, value + defValue)); } } } } } count++; if (written) { continue; } bool withoutTypeParams = true; MemberResolveResult m_rr = null; if (member.Entity != null) { m_rr = Emitter.Resolver.ResolveNode(member.Entity) as MemberResolveResult; if (m_rr != null) { withoutTypeParams = OverloadsCollection.ExcludeTypeParameterForDefinition(m_rr); } } var mname = member.GetName(Emitter, withoutTypeParams); if (TypeInfo.IsEnum && m_rr != null) { mname = Emitter.GetEntityName(m_rr.Member); } bool isValid = Helpers.IsValidIdentifier(mname); if (!isValid) { if (IsObjectLiteral) { mname = "[" + ToJavaScript(mname, Emitter) + "]"; } else { mname = ToJavaScript(mname, Emitter); } } if (IsObjectLiteral) { WriteThis(); if (isValid) { WriteDot(); } Write(mname); Write(" = "); } else { EnsureComma(); XmlToJsDoc.EmitComment(this, member.Entity, null, member.Entity is FieldDeclaration ? member.VarInitializer : null); Write(mname); WriteColon(); } bool close = false; if (isProperty && !IsObjectLiteral && !isAutoProperty) { var oldTempVars = Emitter.TempVariables; BeginBlock(); new VisitorPropertyBlock(Emitter, (PropertyDeclaration)member.Entity).Emit(); WriteNewLine(); EndBlock(); Emitter.Comma = true; Emitter.TempVariables = oldTempVars; continue; } if (constValue is AstType || constValue is IType) { Write("null"); if (!isNullable) { var name = member.GetName(Emitter); bool isValidIdentifier = Helpers.IsValidIdentifier(name); var value = constValue is AstType?Inspector.GetStructDefaultValue((AstType)constValue, Emitter) : Inspector.GetStructDefaultValue((IType)constValue, Emitter); if (!isValidIdentifier) { Injectors.Insert(BeginCounter++, string.Format("this[{0}] = {1};", name.StartsWith("\"") ? name : ToJavaScript(name, Emitter), value)); } else { Injectors.Insert(BeginCounter++, string.Format(name.StartsWith("\"") ? interfaceFormat : format, name, value)); } } } else if (write) { Write(constValue); } else if (writeScript) { WriteScript(constValue); } else { member.Initializer.AcceptVisitor(Emitter); } if (close) { Write(" }"); } if (IsObjectLiteral) { WriteSemiColon(true); } Emitter.Comma = true; } if (count > 0 && objectName != null && !IsObjectLiteral) { WriteNewLine(); EndBlock(); } else if (beginBlock) { Emitter.IsNewLine = writer.IsNewLine; Emitter.ResetLevel(writer.Level); Emitter.Comma = writer.Comma; Emitter.Output.Length = pos; } return(count > 0); }