Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,17 @@ public override void Write(SerializableSyntaxReceiver syntaxReceiver)

Write("public void Set(string propertyName, object target, object value)");
Write("{"); Indent();
var typeName = classObject.FullName.Replace("?", string.Empty);
if (classObject.FieldSymbols.Count > 0 || classObject.PropertySymbols.Count > 0)
{
Write($"var v = ({classObject.FullName.Replace("?", string.Empty)})target;");
if (classObject.ModuleSymbol.TypeKind is TypeKind.Struct)
{
Write($"ref var v = ref System.Runtime.CompilerServices.Unsafe.Unbox<{typeName}>(target);");
}
else
{
Write($"var v = ({typeName})target;");
}
Write("switch (propertyName)");
Write("{"); Indent();
foreach (var field in classObject.FieldSymbols)
Expand All @@ -68,7 +76,7 @@ public override void Write(SerializableSyntaxReceiver syntaxReceiver)

Write("public object Read(string propertyName, object target)");
Write("{"); Indent();
Write($"var v = ({classObject.FullName.Replace("?", string.Empty)})target;");
Write($"var v = ({typeName})target;");
if (classObject.FieldSymbols.Count > 0 || classObject.PropertySymbols.Count > 0)
{
Write("switch (propertyName)");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
HandleEnum(enumSymbol);
}
}
else if (context.Node is ClassDeclarationSyntax classDeclarationSyntax)
else if (context.Node is BaseTypeDeclarationSyntax classDeclarationSyntax)
{
var classSymbol = context.SemanticModel.GetDeclaredSymbol(classDeclarationSyntax)!;
if (classSymbol.GetAttributes().Any())
Expand All @@ -62,7 +62,7 @@ public void OnVisitSyntaxNode(GeneratorSyntaxContext context)

foreach (var type in types.OfType<INamedTypeSymbol>())
{
if (type.TypeKind == TypeKind.Class)
if (type.TypeKind is TypeKind.Class or TypeKind.Struct)
{
AddSerializableClass(type);
}
Expand Down
59 changes: 59 additions & 0 deletions YamlDotNet.Core7AoTCompileTest/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,18 @@
SomeDictionary:
a: 1
b: 2
StructField:
X: 1
Y: 2
Nested:
X: 3
Y: 4
StructProperty:
X: 5
Y: 6
Nested:
X: 7
Y: 8
");

var input = new StringReader(yaml);
Expand Down Expand Up @@ -167,6 +179,12 @@
{
Console.WriteLine(" {0}", s);
}
Console.WriteLine("Structs:");
Console.WriteLine(" StructField: <{0},{1}>", x.StructField.X, x.StructField.Y);
Console.WriteLine(" Nested: <{0},{1}>", x.StructField.Nested.X, x.StructField.Nested.Y);
Console.WriteLine(" StructProperty: <{0},{1}>", x.StructProperty.X, x.StructProperty.Y);
Console.WriteLine(" Nested: <{0},{1}>", x.StructProperty.Nested.X, x.StructProperty.Nested.Y);

Console.WriteLine("==============");
Console.WriteLine("Serialized:");

Expand Down Expand Up @@ -275,6 +293,8 @@ public class PrimitiveTypes
public ICollection<string> SomeCollectionStrings { get; set; }
public object SomeObject { get; set; }
public object SomeDictionary { get; set; }
public MyTestStruct StructField;
public MyTestStruct StructProperty { get; set; }
}

public class InheritedBase
Expand Down Expand Up @@ -329,6 +349,45 @@ public enum EnumMemberedEnum
Hello = 1
}

[YamlSerializable]
public struct MyTestStruct
{
public float X;
public float Y;
public MyTestNestedStruct Nested;

[OnSerializing]
public void Serializing()
{
Console.WriteLine("MyTestStruct: Serializing");
}

[OnSerialized]
public void Serialized()
{
Console.WriteLine("MyTestStruct: Serialized");
}

[OnDeserialized]
public void Deserialized()
{
Console.WriteLine("MyTestStruct: Deserialized");
}

[OnDeserializing]
public void Deserializing()
{
Console.WriteLine("MyTestStruct: Deserializing");
}
}

[YamlSerializable]
public struct MyTestNestedStruct
{
public float X;
public float Y;
}

#pragma warning restore CS8604 // Possible null reference argument.
#pragma warning restore CS8618 // Possible null reference argument.
#pragma warning restore CS8602 // Possible null reference argument.
Loading