我有一个自定义类,该类具有来自外部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
但不起作用。
您仍然需要具有无参数的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);
}