home: Meet a bug seems type inference error
Hi, I meet an “InvalidCastException” when deserializing the serialized content to my data model, it seems that there is incorrect type inference, this is the details of the exception:
System.InvalidCastException HResult=0x80004002 Message=Unable to cast object of type ‘ExtendedXmlSerializerTest.Backend’ to type ‘ExtendedXmlSerializerTest.WorkerBackend’. Source=<Cannot evaluate the exception source> StackTrace: <Cannot evaluate the exception stack trace>
and this is my data model:
{
public class Backend
{
public string Host { get; set; } = "localhost";
public int Port { get; set; } = 8080;
}
public class Portal
{
public string Name { get; set; } = "portal";
public Backend Backend { get; set; } = new Backend();
}
public class WorkerBackend
{
public string ServerUrl { get; set; } = "http://localhost:8080";
}
public class Worker
{
public string Name { get; set; } = "worker";
public WorkerBackend Backend { get; set; } = new WorkerBackend();
}
public class Config
{
public Portal Portal { get; set; } = new Portal();
public Worker Worker { get; set; } = new Worker();
}
}
my serializer implementation:
using ExtendedXmlSerializer.ExtensionModel.Content;
using ExtendedXmlSerializer.ExtensionModel.Xml;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
namespace ExtendedXmlSerializerTest
{
public static class ConfigSerializer
{
public static Config LoadConfig(string filePath)
{
IExtendedXmlSerializer serializer = new ConfigurationContainer()
.Ignore(typeof(List<>).GetProperty(nameof(List<object>.Capacity)))
.UseOptimizedNamespaces()
.Create();
using (var stream = new StreamReader(filePath))
{
return serializer.Deserialize<Config>(stream);
}
}
public static void SaveConfig(Config config, string filePath)
{
var serializer = new ConfigurationContainer()
.Ignore(typeof(List<>).GetProperty(nameof(List<object>.Capacity)))
.UseOptimizedNamespaces()
.Create();
var content = serializer.Serialize(new XmlWriterSettings
{
NewLineChars = Environment.NewLine,
Indent = true,
IndentChars = " ",
Encoding = Encoding.UTF8,
DoNotEscapeUriAttributes = true
}, config);
File.WriteAllText(filePath, content);
}
}
}
and this is my test code:
static class Program
{
static void Main(string[] args)
{
Console.Write("input file name: ");
var fileName = Console.ReadLine();
var config = new Config();
ConfigSerializer.SaveConfig(config, fileName);
var config2 = ConfigSerializer.LoadConfig(fileName);
Console.WriteLine($"portal backend: http://{config2.Portal.Backend.Host}:{config2.Portal.Backend.Port}");
Console.WriteLine($"worker backend: {config2.Worker.Backend}");
}
}
Could anybody help me whether I’m using it the wrong way or something I missed? Thanks.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 15 (8 by maintainers)
@Mike-EEE Really appreciate it.
Yes indeed… looks like there’s something going on with the type resolution. In our tests we usually put the model classes under test as nested classes, which is probably the differentiating factor here. Taking a look into this now.
Hi @Mike-EEE, I created a test repo “https://github.com/DancingCorpse/ExtendedXmlSerializerTest.git” on which you can try it, thanks very much.