Serialization behavior
See original GitHub issueI’m starting integrate ExtendedXmlSerializer to my pet project instead of standard System.Xml. And I have some troubles. Can you suggest how to be in the next situation:
Look at the small repro
internal class Program
{
public static void Main(string[] args)
{
var xs = new ConfigurationContainer().Create();
using (var xw = (XmlWriter.Create(Console.Out)))
xs.Serialize(xw, new SerializedObject());
}
}
public class MyListImpl<T> : List<T>
{
private readonly object _owner;
public MyListImpl(object owner)
{
_owner = owner;
}
}
public class SerializedObject
{
public SerializedObject()
{
MyListImpl = new MyListImpl<string>(this);
MyListImpl.Add("Test");
}
public MyListImpl<string> MyListImpl { get; }
}
I have class look like SerializedObject
(it has the read-only custom list with generic argument property that have not parameter less constructor), but the collection instantiate during SerializedObject construct. It works like a charm in System.Xml but it throw the exception then I try serialize it with default settings using the ExtendedXmlSerializer.
System.InvalidOperationException: The serializer for type 'ProjectForTests.MyListImpl`1[System.String]' could not be found. Please ensure that the type is a valid type can be activated. The default
behavior requires an empty public constructor on the (non-abstract) class to activate.
I have a lot of places with the same code and I can’t and parameter less constructor (I have to guarantee the client what the collection know about the parent).
How can I say to the EXS: “If property type like this: ProjectForTests.MyListImpl<TSomeType> please don’t try to create this. The property already created by the owner instance.”
Thank you.
Issue Analytics
- State:
- Created 5 years ago
- Comments:12 (12 by maintainers)
Top GitHub Comments
Ah! A PR with failing test is EVEN BETTER than that, @cm4ker. 😆 I had to brush off how to do PRs but I was finally able to load your code. Thank you for providing it.
As I mentioned earlier, the parameterized content (by default) (follows protobuf serialization rules found here.
However, these rules are not obvious in the exception that is thrown. So I have created a new issue, using your PR as the base to ensure an exception message is thrown now that details these rules. In your case, the
ChildList
class should look like this:https://github.com/wojtpl2/ExtendedXmlSerializer/blob/d3a582b4d098f0d8aa88c03ec505dc65fa8ec661/test/ExtendedXmlSerializer.Tests/ReportedIssues/Issue218Tests.cs#L89-L94
Here’s the test that shows this passing: https://github.com/wojtpl2/ExtendedXmlSerializer/blob/d3a582b4d098f0d8aa88c03ec505dc65fa8ec661/test/ExtendedXmlSerializer.Tests/ReportedIssues/Issue218Tests.cs#L57-L70
The other item of note is that this did yield a bug that public fields were not working. 😃 So this has been fixed and you will need to use the preview feed to see it in action: https://www.myget.org/F/wojtpl2/api/v3/index.json
Please let me know if you have any further questions around this and I will do my best to further assist.
Yea this is exact what I want. Thank you!