I am trying to use the Application Settings feature of Visual Studios to easy save the setting of my program. One of the class I am trying to serialize contains a number of DenseMatrix objects from the MathNet.Numerics library. The DenseMatrix class does not have a parameter-less constructor so when calling My.Settings.Save() the serialization would crash. I tried replacing the matrices with a Double(,) but that crashed as well. I then tried writing some code to wrap a DenseMatrix as follows but it also failed I am guessing because all the bases classes have to have parameter-less constructors but I am not sure. Is there another logical way to store the matrices that can be automatically serialized by My.Settings.Save?
<SettingsSerializeAs(SettingsSerializeAs.Xml)>
Public Class AvtMatrix
Inherits DenseMatrix
Public Sub New()
My.Base.New(3,3)
End Sub
End Class
Digging into some of the IL, it looks like this uses XmlSerializer
- in which case, the only answer is: you can't - it demands a public parameterless constructor. You can cheat a little , though - with [Obsolete]
; this works, for example:
using System;
using System.IO;
using System.Xml.Serialization;
public class Foo
{
public string Bar { get; set; }
public Foo(string bar)
{
Bar = bar;
}
[Obsolete("You don't look like a serializer", true)]
public Foo()
{
}
}
class Program
{
static void Main()
{
var ser = new XmlSerializer(typeof(Foo));
using (var ms = new MemoryStream())
{
ser.Serialize(ms, new Foo("abc"));
ms.Position = 0;
Foo clone = (Foo)ser.Deserialize(ms);
Console.WriteLine(clone.Bar); // "abc"
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.