我有一个自定义类,它有一些来自外部dll的属性(我无法更改此dll类)
如果属性的类具有空构造函数,则此属性是序列化的,但如果只有一个构造函数必须提供参数,则它无法序列化。
例如,我从这个类中创建了一个实例并尝试序列化,结果是" []"
这是班级
public class Shape : ShapeBase,
{
public Shape(DocumentBase doc, ShapeType shapeType);
public Chart Chart { get; }
public bool ExtrusionEnabled { get; }
public Fill Fill { get; }
public Color FillColor { get; set; }
public bool Filled { get; set; }
public Paragraph FirstParagraph { get; }
public bool HasChart { get; }
public bool HasImage { get; }
public ImageData ImageData { get; }
public Paragraph LastParagraph { get; }
public override NodeType NodeType { get; }
public OleFormat OleFormat { get; }
public bool ShadowEnabled { get; }
public SignatureLine SignatureLine { get; }
public StoryType StoryType { get; }
public Stroke Stroke { get; }
public Color StrokeColor { get; set; }
public bool Stroked { get; set; }
public double StrokeWeight { get; set; }
public TextBox TextBox { get; }
public TextPath TextPath { get; }
public override bool Accept(DocumentVisitor visitor);
}
我尝试像那样序列化
Shape shape = new Shape(_wordDocument, ShapeType.TextPlainText);
string json = JsonConvert.SerializeObject(shape, Formatting.Indented);
但其他课程就像
public class PdfSaveOptions : FixedPageSaveOptions
{
public PdfSaveOptions();
public int BookmarksOutlineLevel { get; set; }
public PdfCompliance Compliance { get; set; }
public bool CreateNoteHyperlinks { get; set; }
public PdfCustomPropertiesExport CustomPropertiesExport { get; set; }
public PdfDigitalSignatureDetails DigitalSignatureDetails { get; set; }
public override DmlEffectsRenderingMode DmlEffectsRenderingMode { get; set; }
public bool DownsampleImages { get; set; }
public DownsampleOptions DownsampleOptions { get; }
.......
}
它是序列化的。
所以我也尝试使用像
这样的选项 ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor
但没有用。
答案 0 :(得分:0)
您仍然需要constructor
没有参数。否则Deserialize
方法无法创建该对象的实例。 Deserialize
尝试实例化对象,然后使用Properties
或Fields
(取决于配置)为其设置数据。
如果您愿意,可以使用构造函数private
或internal
,只要其无参数即可。然后,你有一种在你的课外不存在的构造函数。
public class Shape : ShapeBase,
{
private Shape() {
//here some inits
}
public Shape(DocumentBase doc, ShapeType shapeType);
public Chart Chart { get; }
public bool ExtrusionEnabled { get; }
public Fill Fill { get; }
public Color FillColor { get; set; }
public bool Filled { get; set; }
public Paragraph FirstParagraph { get; }
public bool HasChart { get; }
public bool HasImage { get; }
public ImageData ImageData { get; }
public Paragraph LastParagraph { get; }
public override NodeType NodeType { get; }
public OleFormat OleFormat { get; }
public bool ShadowEnabled { get; }
public SignatureLine SignatureLine { get; }
public StoryType StoryType { get; }
public Stroke Stroke { get; }
public Color StrokeColor { get; set; }
public bool Stroked { get; set; }
public double StrokeWeight { get; set; }
public TextBox TextBox { get; }
public TextPath TextPath { get; }
public override bool Accept(DocumentVisitor visitor);
}