【问题标题】:Error with xml serialization in c# - paramaterless constructorc# - 无参数构造函数中的 xml 序列化错误
【发布时间】:2013-01-11 16:05:20
【问题描述】:

我正在尝试使用以下代码将指纹 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 被引用,所以我不能改变它

【问题讨论】:

标签: c# xml-serialization biometrics


【解决方案1】:

更新

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


上一个答案

错误很明显;您只需要在DataResult&lt;T&gt; 类中添加一个无参数构造函数:

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&lt;Fmd&gt;,无法知道它是否会导致任何问题。

【讨论】:

    【解决方案2】:

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

    进行序列化的方法
    tempFingerPrint = Fmd.SerializeXml(resultConversion.Data);
    

    这是特定于 Digital Persona SDK

    【讨论】:

    • 请告诉我 tempFingerPrint 的类类型
    • tempFingerPrint 数据类型为字符串。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-19
    • 2013-05-08
    相关资源
    最近更新 更多