private string ProcessField(GeneratorExecutionContext context, IFieldSymbol field, ISymbol attribute) { if (!(field.Type is IArrayTypeSymbol)) { context.ReportDiagnostic(Diagnostic.Create(typeError, Location.None, field.Type, field.ToDisplayString())); } AttributeData attributeData = field.GetAttribute(attribute); string xmlAttributeName = attributeData.GetValueOrDefault("Name", field.Name); char delimiter = attributeData.GetValueOrDefault <char>("Delimiter", ','); var source = new StringBuilder(); source.Append($@" [XmlAttribute(""{xmlAttributeName}"")] public string _{xmlAttributeName} {{ get {{ "); AddSerializer(source, field, delimiter); source.Append(@" } set {"); AddDeserializer(source, field, delimiter); source.AppendLine(@" } }"); return(source.ToString()); }
private string ProcessField(GeneratorExecutionContext context, IFieldSymbol field, ISymbol attribute) { if (field.Type.ToDisplayString() != "System.Numerics.Vector3") { context.ReportDiagnostic(Diagnostic.Create(typeError, Location.None, field.Type, field.ToDisplayString())); } AttributeData attributeData = field.GetAttribute(attribute); string xmlAttributeName = attributeData.GetValueOrDefault("Name", field.Name); string fieldName = $"this.{field.Name}"; var source = new StringBuilder(); source.Append($@" [XmlAttribute(""{xmlAttributeName}"")] public string _{xmlAttributeName} {{ get => $""{{{fieldName}.X}},{{{fieldName}.Y}},{{{fieldName}.Z}}""; set {{ string[] split = value.Split(new[] {{',', ' '}}, StringSplitOptions.RemoveEmptyEntries); float x = split.Length > 0 ? float.Parse(split[0]) : 0; float y = split.Length > 1 ? float.Parse(split[1]) : 0; float z = split.Length > 2 ? float.Parse(split[2]) : 0; {fieldName} = new Vector3(x, y, z); }} }}"); return(source.ToString()); }
private string ProcessField(GeneratorExecutionContext context, IFieldSymbol field, ISymbol attribute) { if (!IsNullableValue(field)) { context.ReportDiagnostic(Diagnostic.Create(typeError, Location.None, field.Type, field.ToDisplayString())); } AttributeData attributeData = field.GetAttribute(attribute); string xmlAttributeName = attributeData.GetValueOrDefault("Name", field.Name); string fieldName = $"this.{field.Name}"; string valueType = field.Type.ToDisplayString().TrimEnd('?'); return($@" [XmlAttribute(""{xmlAttributeName}""), DefaultValue(null)] public string _{xmlAttributeName} {{ get => {fieldName}?.ToString(); set => {fieldName} = {valueType}.TryParse(value, out {valueType} n) ? ({field.Type}) n : null; }}"); }