Serialize classes which implement IEnumerable #721
-
|
Hi , public class MyDemoClass: IEnumerable
{
public string Text
{
set;
get;
}
public IEnumerator GetEnumerator()
{
return new MyEnumerator();
}
}When i deserialize the class Text is always null, even if i set it before serializing. Is this the same behaviour like System.Text.Json: dotnet/runtime#1808 ? Thank you |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
|
Like System.Text.Json, Nerdbank.MessagePack always treats types implementing IEnumerable as collections as oppposed to objects with properties. To get back the desired behavior you should either remove the IEnumerable implementation or manually set the type shape kind: [TypeShape(Kind = TypeShapeKind.Object)]
public class MyDemoClass: IEnumerable
{
/* implementation goes here */
} |
Beta Was this translation helpful? Give feedback.
-
Because to generate a shape for a class, all its members must have shapes as well. So GenerateShapeAttribute is transitive. No need then to apply it recursively on every single type. |
Beta Was this translation helpful? Give feedback.
Like System.Text.Json, Nerdbank.MessagePack always treats types implementing IEnumerable as collections as oppposed to objects with properties. To get back the desired behavior you should either remove the IEnumerable implementation or manually set the type shape kind: