c# - Data Serialization on Custom DataSourceControl -



c# - Data Serialization on Custom DataSourceControl -

i writing custom datasourcecontrol have select capability working normal objectdatasource, typename , selectmethod properties.

the info coming typename class saved in files indexed hash of parameter values , contextname. meaning that, everytime gridview requests datasource , same parameter values given, command find corresponding file , load info there. in fact, every different combination of parameter values generate new file data.

this functionality helpful in cases have when info takes long processed , retrieved database , doesn't need live user (its lastly day).

the main difficulty i'm having serialize info coming selectmethod. thing know homecoming type instance of ienumerable. i'm using xmlserializer saving , retrieving info content file, when trying serialize gives me error cannot serialize interface.

this basic code executes selectmethod , serialization part:

//gets select type type selecttype = type.gettype(typename); //gets select method methodinfo selectmethod = selecttype.getmethod(selectmethod, bindingflags.instance | bindingflags.public); //creates new instance of typename class object selectinstance = activator.createinstance(selecttype); //executes select method object selectresult = selectmethod.invoke(selectinstance, parameters); ienumerable list = (ienumerable)selectresult; //create serializer xmlserializer serializer = new xmlserializer(typeof(ienumerable)); //writes xml file using (xmlwriter author = xmlwriter.create(filepath)) { serializer.serialize(writer, list); }

i utilize code deserialize:

//creates xml file xmlserializer deserializer = new xmlserializer(typeof(ienumerable)); ienumerable list = null; //reads xml file using (xmlreader author = xmlreader.create(filepath)) { list = (ienumerable) deserializer.deserialize(writer); }

how can generically serialize/deserialize select method result xml?

update:

i tried using system.web.ui.losformatter serialize/deserialize data. did both actions ienumerable instance had set serializable attribute on entities. noticed important difference in performance compared xmlserializer when retrieving info file. system.web.ui.losformatter 4 times slower @ deserializing on specific test (4mb file). info file half size compared xmlserializer tho. so, me, xmlserializer still best option.

update2:

tried create simple test using servicestack jsonserializer next code:

list<dummy> dummies = new list<dummy>(); dummies.add(new dummy() { name = "name" }); dummies.add(new dummy() { name = "name1" }); dummies.add(new dummy() { name = "name2" }); ienumerable enumerablething = dummies; string filepath = path.combine(server.mappath("~/app_data"), "data2.json"); using (streamwriter author = new streamwriter(filepath)) { jsonserializer.serializetowriter<ienumerable>(enumerablething, writer); } using (streamreader reader = new streamreader(filepath)) { enumerablething = jsonserializer.deserializefromreader<ienumerable>(reader); }

it work serializing, when trying deserializefromreader, gives me error "type system.collections.ienumerable not of type idictionary<,>". there way create work?

well, you've discovered, can't serialize interface...that said, might able work around , still "in dark" actual object types:

first, ienumerable can transmogrified array via cast/toarray combo

var enumerablething = foo.getenumerable(); var asarray = enumerablething.cast<object>().toarray();

second, arrays of "known type" are serializable

var allcontainedtypes = asarray.select(x => x.gettype()).distinct().toarray(); var ser = new xmlserializer(asarray.gettype(), allcontainedtypes);

var ser = new xmlserializer(asarray.gettype());

var sb = new stringbuilder(); using(var sw = new stringwriter(sb)) using(var xw = xmlwriter.create(sw)) ser.serialize(xw, asarray); sb.tostring().dump();

or, together:

void main() { var enumerablething = foo.getenumerable(); var asarray = enumerablething.cast<object>().toarray(); var allcontainedtypes = asarray.select(x => x.gettype()).distinct().toarray(); var ser = new xmlserializer(asarray.gettype(), allcontainedtypes); var sb = new stringbuilder(); using(var sw = new stringwriter(sb)) using(var xw = xmlwriter.create(sw)) ser.serialize(xw, asarray); sb.tostring().dump(); } public class foo { public static ienumerable getenumerable() { homecoming new[] { 1, 2, 3, 4, 5, 6, 7 }; } public static ienumerable getenumerable2() { homecoming new object[] { "1", 2, "bob", 4, null, 6, 7 }; } }

oh, produces format:

<?xml version="1.0" encoding="utf-16"?> <arrayofanytype xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:xsd="http://www.w3.org/2001/xmlschema"> <anytype xsi:type="xsd:int">1</anytype> <anytype xsi:type="xsd:int">2</anytype> <anytype xsi:type="xsd:int">3</anytype> <anytype xsi:type="xsd:int">4</anytype> <anytype xsi:type="xsd:int">5</anytype> <anytype xsi:type="xsd:int">6</anytype> <anytype xsi:type="xsd:int">7</anytype> </arrayofanytype>

edit: deserialization pose problems - however, can akin to:

// to the lowest degree mutual denominator... object[] tempresult; var assemblystuffisfrom = assembly.getexecutingassembly(); var allserializabletypes = assemblystuffisfrom .gettypes() .where(t => t.getcustomattributes(typeof(serializableattribute), true).any()) // todo: add together much filtering can here help trim downwards set .toarray(); var hope = new xmlserializer(typeof(object[]), allserializabletypes); using(var sr = new stringreader(sb.tostring())) using(var xr = xmlreader.create(sr)) tempresult = ((object[])hope.deserialize(xr));

c# asp.net ienumerable xmlserializer datasourcecontrol

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -