C# Json.Net对派生类型集合的反序列化失败
在尝试了几乎所有的解决方案之后,我放弃了,决定寻求你的帮助 我正在尝试序列化/反序列化扩展公共抽象类的对象集合。 序列化运行正常,但反序列化时失败,引发以下异常: Newtonsoft.Json.DLL中的“Newtonsoft.Json.JsonSerializationException” 无法创建Plugins.BaseModel类型的实例。类型是接口或抽象类,无法实例化。路径“Widgets[0]。背景色”,第1行,位置60 在进入代码之前,这里是我试图反序列化的字符串(我缩进了字符串以使其更具可读性!): (C# Json.Net对派生类型集合的反序列化失败,c#,json,inheritance,serialization,json.net,C#,Json,Inheritance,Serialization,Json.net,在尝试了几乎所有的解决方案之后,我放弃了,决定寻求你的帮助 我正在尝试序列化/反序列化扩展公共抽象类的对象集合。 序列化运行正常,但反序列化时失败,引发以下异常: Newtonsoft.Json.DLL中的“Newtonsoft.Json.JsonSerializationException” 无法创建Plugins.BaseModel类型的实例。类型是接口或抽象类,无法实例化。路径“Widgets[0]。背景色”,第1行,位置60 在进入代码之前,这里是我试图反序列化的字符串(我缩进了字符串以
Widgets
是一个可观察集合
)
虽然我没有发布我的JsonSerializerSettings
,因为我在任何设置组合中都会遇到此错误,但下面是我的一小段代码(只关注序列化属性)
(类名称空间插件.BaseModel
)
(classPlugins.widgets.PRIVATE.Dummy.DummyModel
)
(类Plugins.widgets.Battery.SimpleBattery.SimpleBatteryModel
)
如您所见,这两个具体类都继承了基类的属性,并且这些属性被序列化而没有错误。
当我尝试反序列化时,问题就出现了,因为反序列化程序试图创建基类的实例,而不是派生类的实例
有人能解决这个问题吗
编辑:
由于您正在搜索,以下是我的当前设置(基于问题开头链接的答案)
编辑2:
以下是我的项目结构:
Main Solution
\- Main App (WP app project)
\- MainPage.xaml
\- Model (WP C# library project)
\- MainViewModel.cs (contains the collection Widgets that I'm serializing)
\- Plugins (WP C# library project)
\- BaseModel.cs (the main abstract class)
\- widgets.PRIVATE.Dummy.DummyModel.cs (one of the concrete classes)
\- widgets.Battery.SimpleBattery.SimpleBatteryModel.cs (the other concrete class)
其中
主应用程序
引用了模型
哪些引用了插件
,我看不出您是如何序列化和反序列化的,但请确保您也使用相同的JsonSerializerSettings
进行反序列化。它不必是相同的实例,但必须具有相同的选项才能进行反序列化。我不知道如何序列化和反序列化,但请确保使用相同的JsonSerializerSettings
进行反序列化。它不必是同一个实例,但必须具有相同的选项才能进行反序列化。我甚至使用了JsonSerializerSettings
的同一个实例进行序列化和反序列化。嗯,这是一个猜测,因为这个问题可能来自不同的序列化/反序列化设置,我还没有看到代码。您是否尝试过在JsonSerializerSettings中设置TypeNameHandling.Object?已经尝试过了。序列化的输出更改,但例外情况始终相同:\n我已使用当前设置编辑了我的问题。我甚至使用相同的JsonSerializerSettings
实例进行序列化和反序列化。嗯,这是一个猜测,因为这个问题可能来自不同的序列化/反序列化设置,我还没有看到代码。您是否尝试过在JsonSerializerSettings中设置TypeNameHandling.Object?已经尝试过了。序列化输出更改,但例外情况始终相同:\n我已使用当前设置编辑了我的问题。您的TypeNameSerializationBinder
是什么样子的?我现在没有项目,但我复制了此回复中的项目:好的,谢谢。下一个问题——你的插件在不同的程序集中吗?我已经添加了我的项目结构的大致视图,请检查更新的问题。谢谢更新。最后一个问题。您是否尝试过不使用自定义活页夹,而是设置TypeNameAssemblyFormat=FormatterAssemblyStyle.Full
(序列化和反序列化时)?您的TypeNameSerializationBinder
看起来像什么?我现在没有带项目,但我复制了此回复中的项目:好的,谢谢。下一个问题——你的插件在不同的程序集中吗?我已经添加了我的项目结构的大致视图,请检查更新的问题。谢谢更新。最后一个问题。您是否尝试过不使用自定义活页夹,而是设置TypeNameAssemblyFormat=FormatterAssemblyStyle.Full
(序列化和反序列化时)?
[JsonObject(MemberSerialization.OptIn)]
public abstract class BaseModel : ViewModelBase
{
...other stuff...
[JsonProperty]
public Color BackgroundColor
{
get { return _backgroundColor; }
set
{
if (_backgroundColor == value) return;
_backgroundColor = value;
RaisePropertyChanged(() => BackgroundColor);
}
}
[JsonProperty]
public Color ForegroundColor
{
get { return _foregroundColor; }
set
{
if (_foregroundColor == value) return;
_foregroundColor = value;
RaisePropertyChanged(() => ForegroundColor);
}
}
[JsonProperty]
public double BackgroundOpacity
{
get { return _backgroundOpacity; }
set
{
if (value == _backgroundOpacity) return;
_backgroundOpacity = value;
_backgroundColor.A = (byte) (_backgroundOpacity*255);
RaisePropertyChanged(() => BackgroundOpacity);
RaisePropertyChanged(() => BackgroundColor);
}
}
[JsonProperty]
public Thickness WidgetPosition
{
get { return _widgetPosition; }
set
{
if (value == _widgetPosition) return;
_widgetPosition = value;
RaisePropertyChanged(() => WidgetPosition);
}
}
...other stuff...
}
[JsonObject(MemberSerialization.OptIn)]
public class DummyModel : BaseModel
{
... other stuff...
[JsonProperty]
public string Text
{
get { return _text; }
set
{
if (_text == value) return;
_text = value;
RaisePropertyChanged(() => Text);
}
}
... other stuff ...
}
[JsonObject(MemberSerialization.OptIn)]
public class SimpleBatteryModel : BaseModel
{
... other stuff ...
}
_settings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto, Binder = new TypeNameSerializationBinder("Plugins.{0}, Plugins")};
Main Solution
\- Main App (WP app project)
\- MainPage.xaml
\- Model (WP C# library project)
\- MainViewModel.cs (contains the collection Widgets that I'm serializing)
\- Plugins (WP C# library project)
\- BaseModel.cs (the main abstract class)
\- widgets.PRIVATE.Dummy.DummyModel.cs (one of the concrete classes)
\- widgets.Battery.SimpleBattery.SimpleBatteryModel.cs (the other concrete class)