c# - How to deserialize an immutable data structure? -


how deserialize yaml immutable data structure?

e.g. have yaml:

value: somestring number: 99 

and data structure:

public class mydata {     public mydata(string value, int number)     {         value = value;         number = number;     }      public string value { get; }     public int number { get; } } 

for i'd use constructor. somehow i'd need first retrieve dictionary<string, object> parsed yaml respecting class (so 99 int, not string), scan type appropriate constructor,

use formatterservices.getuninitializedobject api (this not invoke constructors @ all) , use reflection set fields.

code example:

 var instance = formatterservices.getuninitializedobject(typeof(mydata));  var flags = bindingflags.nonpublic | bindingflags.instance;  var type = typeof(mydata);  var stringfield = type.getfield("_value", flags);   stringfield.setvalue(instance, "somestring");   var numberfield = type.getfield("_number", flags);  numberfield.setvalue(instance, 99);   mydata data = (mydata)instance; 

Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -