.net - How do I XML Serialize a object without a parameter-less constructor? -
i trying use application settings feature of visual studios easy save setting of program. 1 of class trying serialize contains number of densematrix objects mathnet.numerics library. densematrix class not have parameter-less constructor when calling my.settings.save() serialization crash. tried replacing matrices double(,) crashed well. tried writing code wrap densematrix follows failed guessing because bases classes have have parameter-less constructors not sure. there logical way store matrices can automatically serialized my.settings.save?
 <settingsserializeas(settingsserializeas.xml)>  public class avtmatrix     inherits densematrix     public sub new()        my.base.new(3,3)     end sub   end class      
digging of il, looks uses xmlserializer - in case, answer is: can't - demands public parameterless constructor. can cheat little, though - [obsolete]; works, 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 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"         }     } }      
Comments
Post a Comment