Meet a bug seems type inference error
See original GitHub issueHi, 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.
Issue Analytics
- State:
- Created 4 years ago
- Comments:15 (8 by maintainers)
Top Results From Across the Web
Type inference failed. This indicates an invalid graph that ...
Type inference failed. This indicates an invalid graph that escaped type checking. Error message: INVALID_ARGUMENT #57052.
Read more >vscode show the wrong type inference error when using ...
It is a bug or the config is wrong? Now it report error with red wavy line. Can I only use store =...
Read more >Finding User/Kernel Pointer Bugs With Type Inference
user/kernel pointer bugs using type-qualifier inference,. and we apply this method to the Linux kernel using. CQUAL, a type-qualifier inference tool.
Read more >New Type Inference Error in 1.3.40 - Support
I'm aware that there is a new type inference algorithm in 1.3.40 ... It seems that you are mixing Java and Kotlin classes...
Read more >What's the tradeoff for type inference?
Since the type is not explicitly annotated, it can at times make the code harder to read - leading to more bugs. Properly...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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.