Skip to content

Commit

Permalink
Support inherited objects
Browse files Browse the repository at this point in the history
  • Loading branch information
EdwardCooke committed Jan 29, 2023
1 parent 61573a9 commit 72641d1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 16 deletions.
36 changes: 20 additions & 16 deletions YamlDotNet.Analyzers.StaticGenerator/ClassSyntaxReceiver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,26 +59,30 @@ public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
classObject = new ClassObject(className, classSymbol);
Classes[className] = classObject;
}
var members = classSymbol.GetMembers();
foreach (var member in members)
while (classSymbol != null)
{
if (member.IsStatic ||
member.DeclaredAccessibility != Accessibility.Public ||
member.GetAttributes().Any(x => x.AttributeClass!.ToDisplayString() == "YamlDotNet.Serialization.YamlIgnoreAttribute"))
var members = classSymbol.GetMembers();
foreach (var member in members)
{
continue;
}
if (member.IsStatic ||
member.DeclaredAccessibility != Accessibility.Public ||
member.GetAttributes().Any(x => x.AttributeClass!.ToDisplayString() == "YamlDotNet.Serialization.YamlIgnoreAttribute"))
{
continue;
}

if (member is IPropertySymbol propertySymbol)
{
classObject.PropertySymbols.Add(propertySymbol);
CheckForSupportedGeneric(propertySymbol.Type);
}
else if (member is IFieldSymbol fieldSymbol)
{
classObject.FieldSymbols.Add(fieldSymbol);
CheckForSupportedGeneric(fieldSymbol.Type);
if (member is IPropertySymbol propertySymbol)
{
classObject.PropertySymbols.Add(propertySymbol);
CheckForSupportedGeneric(propertySymbol.Type);
}
else if (member is IFieldSymbol fieldSymbol)
{
classObject.FieldSymbols.Add(fieldSymbol);
CheckForSupportedGeneric(fieldSymbol.Type);
}
}
classSymbol = classSymbol.BaseType;
}
}
}
Expand Down
19 changes: 19 additions & 0 deletions YamlDotNet.Core7AoTCompileTest/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Security.Cryptography.X509Certificates;
using YamlDotNet.Core;
using YamlDotNet.Core.Events;
using YamlDotNet.Serialization;
Expand Down Expand Up @@ -62,6 +63,9 @@
MyList:
- a
- b
Inherited:
Inherited: hello
NotInherited: world
";

var input = new StringReader(yaml);
Expand Down Expand Up @@ -117,6 +121,9 @@
Console.WriteLine("MyList = <{0}>", value);
}
}
Console.WriteLine("Inherited == null: <{0}>", x.Inherited == null);
Console.WriteLine("Inherited.Inherited: <{0}>", x.Inherited?.Inherited);
Console.WriteLine("Inherited.NotInherited: <{0}>", x.Inherited?.NotInherited);

Console.WriteLine("==============");
Console.WriteLine("Serialized:");
Expand Down Expand Up @@ -181,6 +188,18 @@ public class PrimitiveTypes
public MyArray? MyArray { get; set; }
public Dictionary<string, string>? MyDictionary { get; set; }
public List<string>? MyList { get; set; }
public Inherited Inherited { get; set; }
}

public class InheritedBase
{
public string Inherited { get; set; }
}

[YamlSerializable]
public class Inherited : InheritedBase
{
public string NotInherited { get; set; }
}

public enum MyTestEnum
Expand Down

0 comments on commit 72641d1

Please sign in to comment.