0

我正在尝试使用以下代码将指纹 FMD 序列化为 XML,但出现错误:

错误: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);
                //

类 DataResult 被引用,所以我不能改变它

4

2 回答 2

1

更新

如果您无权访问DataResult<T>该类,那么您可能必须采取一种稍微不同的方法,并用一个不同的、可序列化的类来包装这个类。你可以在这里找到一个完整的例子:


上一个答案

错误很明显;你只需要在类中添加一个无参数的构造函数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.
    }
}

至于添加默认构造函数的含义,不知道是什么

FeatureExtraction.CreateFmdFromFid(...)

正在创建DataResult<Fmd>,无法知道它是否会导致任何问题。

于 2013-01-11T16:09:13.237 回答
1

感谢 Cory,这是一个有用的答案,但是在此示例中,还有另一种使用序列化的方法

tempFingerPrint = Fmd.SerializeXml(resultConversion.Data);

这是特定于 Digital Persona SDK

于 2013-01-12T10:03:40.727 回答