简体   繁体   English

c#中的xml序列化错误 - 无参数构造函数

[英]Error with xml serialization in c# - paramaterless constructor

I am trying to serialse a fingerprint FMD to XML using the code below, but get an error: 我正在尝试使用下面的代码将指纹FMD序列化为XML,但是收到错误:

Error: DPUruNet.DataResult`1[DPUruNet.Fmd] cannot be serialized because it does not have a parameterless constructor. 错误:DPUruNet.DataResult`1 [DPUruNet.Fmd]无法序列化,因为它没有无参数构造函数。

  public void storePrint(DataResult<Fmd> resultConversion)
        {
                //store fingerprint as byte and insert to server------------
                 using (StreamWriter myWriter = new StreamWriter("test.txt", false))
                {

                    System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(resultConversion.GetType());
                    x.Serialize(myWriter, resultConversion);
                }
                MessageBox.Show("Fingerprint Stored!");

                //------------------------------------------------------------
        }
        private void OnCaptured(CaptureResult captureResult)
        {
            try
            {
                // Check capture quality and throw an error if bad.
                if (!_sender.CheckCaptureResult(captureResult)) return;

                count++;

                DataResult<Fmd> resultConversion = FeatureExtraction.CreateFmdFromFid(captureResult.Data, Constants.Formats.Fmd.ANSI);

                SendMessage(Action.SendMessage, "A finger was captured.  \r\nCount:  " + (count));

                if (resultConversion.ResultCode != Constants.ResultCode.DP_SUCCESS)
                {
                    _sender.Reset = true;
                    throw new Exception(resultConversion.ResultCode.ToString());
                }

                preenrollmentFmds.Add(resultConversion.Data);
                //--------------------CALL METHOD
                storePrint(resultConversion);
                //

The class DataResult is being referenced, so I can not alter it 正在引用类DataResult,所以我不能改变它

UPDATE UPDATE

If you don't have access to the DataResult<T> class, then you might have to take a slightly different approach and wrap this class with a different, serializable one. 如果您无法访问DataResult<T>类,则可能需要采用稍微不同的方法,并使用不同的可序列化类来包装此类。 You can find a full example here: 你可以在这里找到一个完整的例子:


Previous Answer 上一个答案

The error is clear; 错误很明显; you just need to add a parameterless constructor to the DataResult<T> class: 您只需DataResult<T>类添加无参数构造函数:

public class DataResult<T>
{
    // Add a default constructor (public visibility, no parameters)
    public DataResult() 
    {
        // You can still define a method body if you wish,
        // no restrictions there. Just don't do anything that
        // could jeopardize the (de)serialization.
    }
}

As for the implications of adding a default constructor, without knowing what 至于添加默认构造函数的含义,不知道是什么

FeatureExtraction.CreateFmdFromFid(...)

is doing to create the DataResult<Fmd> , it would be impossible to know whether it would cause any issues. 正在创建DataResult<Fmd> ,不可能知道它是否会导致任何问题。

Thanks to Cory, that is a useful answer, however in this example there is another way of serializing using 感谢Cory,这是一个有用的答案,但是在这个例子中还有另一种序列化使用方法

tempFingerPrint = Fmd.SerializeXml(resultConversion.Data);

this is specific to the Digital Persona SDK 这是Digital Persona SDK特有的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM