元组无法序列化,因为它没有无参数构造函数
Tuple cannot be serialized because it does not have a parameterless constructor
我正在尝试序列化下面提供的 QDatatables class 的实例,但出现错误:
'System.InvalidOperationException' 类型的异常发生在 System.Xml.dll 但未在用户代码中处理
附加信息:反映类型 'System.Collections.ObjectModel.ObservableCollection`1[DataRetrieval.Model.QDatatable]'.
时出错
StackTrace:at System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers(StructMapping 映射、StructModel 模型、布尔 openModel、字符串类型名称、RecursionLimiter 限制器)
在 System.Xml.Serialization.XmlReflectionImporter.ImportStructLikeMapping(StructModel 模型、字符串 ns、布尔 openModel、XmlAttributes a、RecursionLimiter 限制器)
在 System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel 模型、String ns、ImportContext 上下文、String dataType、XmlAttributes a、布尔重复、布尔 openModel、RecursionLimiter 限制器)
内部异常:{"System.Tuple`2[System.String,System.String] cannot be serialized because it does not have a parameterless constructor."}
谁能帮忙找到丢失的东西?
我的序列化函数:
public string serialize()
{
StringWriter sw = new StringWriter();
XmlSerializer s = new XmlSerializer(this.GetType());
s.Serialize(sw, this);
return sw.ToString();
}
QDatatables class:
[Serializable()]
public class QDatatables : BindableBase
{
private ObservableCollection<QDatatable> _list;
public ObservableCollection<QDatatable> List
{
get { return _list ?? (_list=new ObservableCollection<QDatatable>()); }
set { SetProperty(ref _list, value); }
}
public string serialize()
{
StringWriter sw = new StringWriter();
XmlSerializer s = new XmlSerializer(typeof(ObservableCollection<QDatatable>));
s.Serialize(sw, List);
return sw.ToString();
}
}
QDatatable Class
[Serializable()]
public class QDatatable : BindableBase
{
private int _id;
public int ID
{
get { return _id; }
set { SetProperty(ref _id, value); }
}
private String _name;
public String Name
{
get { return _name; }
set { SetProperty(ref _name, value); }
}
private WhereParams _params;
public WhereParams Params
{
get { return _params; }
set { SetProperty(ref _params, value); }
}
private bool _isexpanded;
public bool IsExpanded
{
get { return _isexpanded; }
set { SetProperty(ref _isexpanded, value); }
}
}
WhereParam Class:
public class WhereParams : BindableBase
{
private Dictionary<int, WhereParam> _dictionaryIdToWhereParam;
private ObservableCollection<WhereParam> _list;
public ObservableCollection<WhereParam> List
{
get { return _list ?? (_list = new ObservableCollection<WhereParam>()); }
set { SetProperty(ref _list, value); }
}
public WhereParam GetById(int id)
{
return List.First(w => w.ID == id);
}
public string serialize()
{
StringWriter sw = new StringWriter();
XmlSerializer s = new XmlSerializer(this.GetType());
s.Serialize(sw, this);
return sw.ToString();
}
}
[Serializable()]
public class WhereParam:BindableBase
{
private int _id;
public int ID
{
get { return _id; }
set { SetProperty(ref _id, value); }
}
private string _name;
public string Name
{
get { return _name; }
set { SetProperty(ref _name, value); }
}
private ParamType _type;
public ParamType Type
{
get { return _type; }
set { SetProperty(ref _type, value); }
}
}
参数类型Class:
[XmlInclude(typeof(DateTimeType))]
[XmlInclude(typeof(StringType))]
[XmlInclude(typeof(IntType))]
[XmlInclude(typeof(FloatgType))]
[XmlInclude(typeof(BoolType))]
[XmlInclude(typeof(NullableBoolType))]
[XmlInclude(typeof(ListMulti))]
[XmlInclude(typeof(ListSingle))]
[XmlInclude(typeof(StringMulti))]
public class ParamType: BindableBase
{
private int _typeID;
public int TypeID
{
get { return _typeID; }
set { SetProperty(ref _typeID, value); }
}
private ParamTypeEnum _typeName;
public ParamTypeEnum TypeName
{
get { return _typeName; }
set { SetProperty(ref _typeName, value); }
}
}
public class DateTimeType : ParamType
{
private DateTime? _datefrom;
public DateTime? Datefrom
{
get { return _datefrom; }
set { SetProperty(ref _datefrom, value); }
}
private DateTime? _dateTo;
public DateTime? DateTo
{
get { return _dateTo; }
set {
SetProperty(ref _dateTo, value); }
}
}
public class StringType : ParamType
{
private string _stringContent;
public string StringContent
{
get { return _stringContent; }
set {
SetProperty(ref _stringContent, value); }
}
}
public class IntType : ParamType
{
private int _intContent;
public int IntContent
{
get { return _intContent; }
set { SetProperty(ref _intContent, value); }
}
}
public class FloatgType : ParamType
{
private float _floatContent;
public float FloatContent
{
get { return _floatContent; }
set { SetProperty(ref _floatContent, value); }
}
}
public class BoolType : ParamType
{
private bool _boolTypeValue;
public bool BoolTypeValue
{
get { return _boolTypeValue; }
set { SetProperty(ref _boolTypeValue, value); }
}
}
public class NullableBoolType : ParamType
{
private bool? _nullableBoolTypeValue;
public bool? NullableBoolTypeValue
{
get { return _nullableBoolTypeValue; }
set { SetProperty(ref _nullableBoolTypeValue, value); }
}
}
public class ListMulti : ParamType
{
private ObservableCollection<Tuple<string, string>> _listMultiItems;
public ObservableCollection<Tuple<string, string>> ListMultiItems
{
get { return _listMultiItems; }
set { SetProperty(ref _listMultiItems, value); }
}
private ObservableCollection<Tuple<string, string>> _selectedListMulti;
public ObservableCollection<Tuple<string, string>> SelectedItemsListMulti
{
get { return _selectedListMulti ?? (_selectedListMulti = new ObservableCollection<Tuple<string,string>>()); }
set {
SetProperty(ref _selectedListMulti, value); }
}
}
public class ListSingle : ParamType
{
private ObservableCollection<Tuple<string, string>> _listSingleItems;
public ObservableCollection<Tuple<string, string>> ListSingleItems
{
get { return _listSingleItems; }
set { SetProperty(ref _listSingleItems, value); }
}
//private ObservableCollection<Tuple<string, string>> _listSingleItems;
//public ObservableCollection<Tuple<string, string>> ListSingleItems
//{
// get { return _listSingleItems; }
// set { SetProperty(ref _listSingleItems, value); }
//}
}
public class StringMulti : ParamType
{
private string _listMultiCollection;
public string ListMultiCollection
{
get { return _listMultiCollection; }
set { SetProperty(ref _listMultiCollection, value); }
}
}
public enum ParamTypeEnum
{
boolType,
nullableboolType,
intType,
floatType,
stringType,
datetimeType,
listmultiType,
stringlistmultiType,
};
问题是 class Tuple<T1, T2>
被你的 class ListMulti
等使用,没有默认构造函数,因为元组只能公开通过 Tuple.Create()
. XmlSerializer
, however, requires classes to have parameterless default constructors 创建,如果遇到没有默认构造函数的类型,将抛出异常。这是您看到的异常。
一种解决方法是向您的 class 添加代理属性,重新打包并 return 其数据以可序列化的形式出现。然后,用 XmlIgnore
标记 "real" 属性。对于 ListMulti
持有的字符串元组集合,string [][]
代理 属性 是有意义的:
public class ListMulti : ParamType
{
[XmlArray("ListMultiItems")]
[XmlArrayItem("Pair")]
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never), DebuggerBrowsable(DebuggerBrowsableState.Never)]
public string[][] SerializableListMultiItems
{
get
{
return ListMultiItems.ToPairArray();
}
set
{
ListMultiItems.SetFromPairArray(value);
}
}
[XmlArray("SelectedItemsListMulti")]
[XmlArrayItem("Pair")]
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never), DebuggerBrowsable(DebuggerBrowsableState.Never)]
public string[][] SerializableSelectedItemsListMulti
{
get
{
return SelectedItemsListMulti.ToPairArray();
}
set
{
SelectedItemsListMulti.SetFromPairArray(value);
}
}
private ObservableCollection<Tuple<string, string>> _listMultiItems = new ObservableCollection<Tuple<string, string>>();
[XmlIgnore]
public ObservableCollection<Tuple<string, string>> ListMultiItems
{
get { return _listMultiItems; }
set { SetProperty(ref _listMultiItems, value); }
}
private ObservableCollection<Tuple<string, string>> _selectedListMulti;
[XmlIgnore]
public ObservableCollection<Tuple<string, string>> SelectedItemsListMulti
{
get { return _selectedListMulti ?? (_selectedListMulti = new ObservableCollection<Tuple<string, string>>()); }
set
{
SetProperty(ref _selectedListMulti, value);
}
}
}
使用扩展方法:
public static class TupleExtensions
{
public static T[][] ToPairArray<T>(this IEnumerable<Tuple<T, T>> collection)
{
return collection == null ? null : collection.Select(t => new[] { t.Item1, t.Item2 }).ToArray();
}
public static void SetFromPairArray<T>(this ICollection<Tuple<T, T>> collection, T[][] array)
{
if (collection == null)
throw new ArgumentNullException();
collection.Clear();
foreach (var pair in array)
{
if (pair.Length != 2)
throw new ArgumentException("Inner array did not have length 2");
collection.Add(Tuple.Create(pair[0], pair[1]));
}
}
}
您需要对 ListSingle
进行类似的修复。
我正在尝试序列化下面提供的 QDatatables class 的实例,但出现错误:
'System.InvalidOperationException' 类型的异常发生在 System.Xml.dll 但未在用户代码中处理 附加信息:反映类型 'System.Collections.ObjectModel.ObservableCollection`1[DataRetrieval.Model.QDatatable]'.
时出错StackTrace:at System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers(StructMapping 映射、StructModel 模型、布尔 openModel、字符串类型名称、RecursionLimiter 限制器) 在 System.Xml.Serialization.XmlReflectionImporter.ImportStructLikeMapping(StructModel 模型、字符串 ns、布尔 openModel、XmlAttributes a、RecursionLimiter 限制器) 在 System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel 模型、String ns、ImportContext 上下文、String dataType、XmlAttributes a、布尔重复、布尔 openModel、RecursionLimiter 限制器)
内部异常:{"System.Tuple`2[System.String,System.String] cannot be serialized because it does not have a parameterless constructor."}
谁能帮忙找到丢失的东西?
我的序列化函数:
public string serialize()
{
StringWriter sw = new StringWriter();
XmlSerializer s = new XmlSerializer(this.GetType());
s.Serialize(sw, this);
return sw.ToString();
}
QDatatables class:
[Serializable()]
public class QDatatables : BindableBase
{
private ObservableCollection<QDatatable> _list;
public ObservableCollection<QDatatable> List
{
get { return _list ?? (_list=new ObservableCollection<QDatatable>()); }
set { SetProperty(ref _list, value); }
}
public string serialize()
{
StringWriter sw = new StringWriter();
XmlSerializer s = new XmlSerializer(typeof(ObservableCollection<QDatatable>));
s.Serialize(sw, List);
return sw.ToString();
}
}
QDatatable Class
[Serializable()]
public class QDatatable : BindableBase
{
private int _id;
public int ID
{
get { return _id; }
set { SetProperty(ref _id, value); }
}
private String _name;
public String Name
{
get { return _name; }
set { SetProperty(ref _name, value); }
}
private WhereParams _params;
public WhereParams Params
{
get { return _params; }
set { SetProperty(ref _params, value); }
}
private bool _isexpanded;
public bool IsExpanded
{
get { return _isexpanded; }
set { SetProperty(ref _isexpanded, value); }
}
}
WhereParam Class:
public class WhereParams : BindableBase
{
private Dictionary<int, WhereParam> _dictionaryIdToWhereParam;
private ObservableCollection<WhereParam> _list;
public ObservableCollection<WhereParam> List
{
get { return _list ?? (_list = new ObservableCollection<WhereParam>()); }
set { SetProperty(ref _list, value); }
}
public WhereParam GetById(int id)
{
return List.First(w => w.ID == id);
}
public string serialize()
{
StringWriter sw = new StringWriter();
XmlSerializer s = new XmlSerializer(this.GetType());
s.Serialize(sw, this);
return sw.ToString();
}
}
[Serializable()]
public class WhereParam:BindableBase
{
private int _id;
public int ID
{
get { return _id; }
set { SetProperty(ref _id, value); }
}
private string _name;
public string Name
{
get { return _name; }
set { SetProperty(ref _name, value); }
}
private ParamType _type;
public ParamType Type
{
get { return _type; }
set { SetProperty(ref _type, value); }
}
}
参数类型Class:
[XmlInclude(typeof(DateTimeType))]
[XmlInclude(typeof(StringType))]
[XmlInclude(typeof(IntType))]
[XmlInclude(typeof(FloatgType))]
[XmlInclude(typeof(BoolType))]
[XmlInclude(typeof(NullableBoolType))]
[XmlInclude(typeof(ListMulti))]
[XmlInclude(typeof(ListSingle))]
[XmlInclude(typeof(StringMulti))]
public class ParamType: BindableBase
{
private int _typeID;
public int TypeID
{
get { return _typeID; }
set { SetProperty(ref _typeID, value); }
}
private ParamTypeEnum _typeName;
public ParamTypeEnum TypeName
{
get { return _typeName; }
set { SetProperty(ref _typeName, value); }
}
}
public class DateTimeType : ParamType
{
private DateTime? _datefrom;
public DateTime? Datefrom
{
get { return _datefrom; }
set { SetProperty(ref _datefrom, value); }
}
private DateTime? _dateTo;
public DateTime? DateTo
{
get { return _dateTo; }
set {
SetProperty(ref _dateTo, value); }
}
}
public class StringType : ParamType
{
private string _stringContent;
public string StringContent
{
get { return _stringContent; }
set {
SetProperty(ref _stringContent, value); }
}
}
public class IntType : ParamType
{
private int _intContent;
public int IntContent
{
get { return _intContent; }
set { SetProperty(ref _intContent, value); }
}
}
public class FloatgType : ParamType
{
private float _floatContent;
public float FloatContent
{
get { return _floatContent; }
set { SetProperty(ref _floatContent, value); }
}
}
public class BoolType : ParamType
{
private bool _boolTypeValue;
public bool BoolTypeValue
{
get { return _boolTypeValue; }
set { SetProperty(ref _boolTypeValue, value); }
}
}
public class NullableBoolType : ParamType
{
private bool? _nullableBoolTypeValue;
public bool? NullableBoolTypeValue
{
get { return _nullableBoolTypeValue; }
set { SetProperty(ref _nullableBoolTypeValue, value); }
}
}
public class ListMulti : ParamType
{
private ObservableCollection<Tuple<string, string>> _listMultiItems;
public ObservableCollection<Tuple<string, string>> ListMultiItems
{
get { return _listMultiItems; }
set { SetProperty(ref _listMultiItems, value); }
}
private ObservableCollection<Tuple<string, string>> _selectedListMulti;
public ObservableCollection<Tuple<string, string>> SelectedItemsListMulti
{
get { return _selectedListMulti ?? (_selectedListMulti = new ObservableCollection<Tuple<string,string>>()); }
set {
SetProperty(ref _selectedListMulti, value); }
}
}
public class ListSingle : ParamType
{
private ObservableCollection<Tuple<string, string>> _listSingleItems;
public ObservableCollection<Tuple<string, string>> ListSingleItems
{
get { return _listSingleItems; }
set { SetProperty(ref _listSingleItems, value); }
}
//private ObservableCollection<Tuple<string, string>> _listSingleItems;
//public ObservableCollection<Tuple<string, string>> ListSingleItems
//{
// get { return _listSingleItems; }
// set { SetProperty(ref _listSingleItems, value); }
//}
}
public class StringMulti : ParamType
{
private string _listMultiCollection;
public string ListMultiCollection
{
get { return _listMultiCollection; }
set { SetProperty(ref _listMultiCollection, value); }
}
}
public enum ParamTypeEnum
{
boolType,
nullableboolType,
intType,
floatType,
stringType,
datetimeType,
listmultiType,
stringlistmultiType,
};
问题是 class Tuple<T1, T2>
被你的 class ListMulti
等使用,没有默认构造函数,因为元组只能公开通过 Tuple.Create()
. XmlSerializer
, however, requires classes to have parameterless default constructors 创建,如果遇到没有默认构造函数的类型,将抛出异常。这是您看到的异常。
一种解决方法是向您的 class 添加代理属性,重新打包并 return 其数据以可序列化的形式出现。然后,用 XmlIgnore
标记 "real" 属性。对于 ListMulti
持有的字符串元组集合,string [][]
代理 属性 是有意义的:
public class ListMulti : ParamType
{
[XmlArray("ListMultiItems")]
[XmlArrayItem("Pair")]
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never), DebuggerBrowsable(DebuggerBrowsableState.Never)]
public string[][] SerializableListMultiItems
{
get
{
return ListMultiItems.ToPairArray();
}
set
{
ListMultiItems.SetFromPairArray(value);
}
}
[XmlArray("SelectedItemsListMulti")]
[XmlArrayItem("Pair")]
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never), DebuggerBrowsable(DebuggerBrowsableState.Never)]
public string[][] SerializableSelectedItemsListMulti
{
get
{
return SelectedItemsListMulti.ToPairArray();
}
set
{
SelectedItemsListMulti.SetFromPairArray(value);
}
}
private ObservableCollection<Tuple<string, string>> _listMultiItems = new ObservableCollection<Tuple<string, string>>();
[XmlIgnore]
public ObservableCollection<Tuple<string, string>> ListMultiItems
{
get { return _listMultiItems; }
set { SetProperty(ref _listMultiItems, value); }
}
private ObservableCollection<Tuple<string, string>> _selectedListMulti;
[XmlIgnore]
public ObservableCollection<Tuple<string, string>> SelectedItemsListMulti
{
get { return _selectedListMulti ?? (_selectedListMulti = new ObservableCollection<Tuple<string, string>>()); }
set
{
SetProperty(ref _selectedListMulti, value);
}
}
}
使用扩展方法:
public static class TupleExtensions
{
public static T[][] ToPairArray<T>(this IEnumerable<Tuple<T, T>> collection)
{
return collection == null ? null : collection.Select(t => new[] { t.Item1, t.Item2 }).ToArray();
}
public static void SetFromPairArray<T>(this ICollection<Tuple<T, T>> collection, T[][] array)
{
if (collection == null)
throw new ArgumentNullException();
collection.Clear();
foreach (var pair in array)
{
if (pair.Length != 2)
throw new ArgumentException("Inner array did not have length 2");
collection.Add(Tuple.Create(pair[0], pair[1]));
}
}
}
您需要对 ListSingle
进行类似的修复。