tag:blogger.com,1999:blog-2744072865491516720.post3420064704279382558..comments2023-05-03T06:35:33.259-04:00Comments on Higher Logics: Reflection, Attributes and ParameterizationSandro Magihttp://www.blogger.com/profile/05446177882449578817noreply@blogger.comBlogger3125tag:blogger.com,1999:blog-2744072865491516720.post-55160468843814113052008-11-06T17:11:00.000-05:002008-11-06T17:11:00.000-05:00Another Pro of the alternate serialization design ...Another Pro of the alternate serialization design is the clearer versioning semantics; .NETs versioning is rather complicated by comparison.Sandro Magihttps://www.blogger.com/profile/05446177882449578817noreply@blogger.comtag:blogger.com,1999:blog-2744072865491516720.post-48986764329552973892008-11-06T10:20:00.000-05:002008-11-06T10:20:00.000-05:00There are two ways to provide serialize via interf...There are two ways to provide serialize via interfaces: via positive or negative information.<BR/><BR/>The NonSerialized attribute is negative information, and I was confusing it with the implementation for positive information, so here's the actual interface:<BR/><BR/>interface INonSerialized {<BR/> void Field(string fieldName);<BR/>}<BR/><BR/>A serializer would still use reflection, or some other means to access the object internals and would maintain the set of fields it should skip:<BR/><BR/>class Serializer : INonSerialized {<BR/> ISet skip = new Set();// any set semantics will do<BR/><BR/>void Serialize(object obj, Stream s) {<BR/> if (obj is IUnserializableMembers) {<BR/> (obj as IUnserializableMembers).Unserializable(this);<BR/> }<BR/> foreach(var field in o.GetType().GetFields()) {<BR/> if (skip.Contains(field.Name)) {<BR/> // do not serialize<BR/> } else { ... }<BR/> }<BR/>}<BR/><BR/>void Field(string fieldName) {<BR/> skip.Add(fieldName);<BR/>}<BR/>}<BR/><BR/>I don't like this implementation much, and I prefer the converse:<BR/><BR/>interface ISerializer {<BR/> void Version(string version);<BR/> void Field<T>(ref T value);<BR/> void Deleted<T>(string version);<BR/>}<BR/>interface ISerializable {<BR/> void Serialize(ISerializer s);<BR/>}<BR/><BR/>This is a minimal serialization interface that I think can replace 90% of the standard serializer, including versioning, and yet produce binaries that are more compact. Example usage:<BR/><BR/>class Test : ISerializable {<BR/> int i;<BR/> string name;<BR/> ...<BR/> public void Serialize(ISerializer s) {<BR/> s.Version("1.0.0");<BR/> s.Field(ref i);<BR/> s.Field(ref name);<BR/> }<BR/>}<BR/><BR/>ISerializable implementors must ensure that they do not reorder Field calls. New fields must go at the end. Any Field calls to be deleted should be replaced with a Deleted call, along with the version number at which the field was deleted.<BR/><BR/>Pros:<BR/>1. Runs at full speed, ie. no reflection penalty.<BR/>2. Statically typed.<BR/><BR/>Cons:<BR/>1. Client must implement the interface (though compilers can easily derive the necessary function).<BR/>2. Slightly higher implementation burden.<BR/><BR/>It's an interesting alternative design at the very least.Sandro Magihttps://www.blogger.com/profile/05446177882449578817noreply@blogger.comtag:blogger.com,1999:blog-2744072865491516720.post-63969228143392410352008-11-06T07:28:00.000-05:002008-11-06T07:28:00.000-05:00Hi, can you give a code example for a hypotethical...Hi, can you give a code example for a hypotethical code for using<BR/>the first use case -- a serializer using your proposal instead of<BR/>reflection+attributes.<BR/><BR/>An example reflection+attribute using version may be:<BR/><BR/>void serialize(Object o) {<BR/> foreach(var field in o.GetType().GetFields()) {<BR/> if(field.Attributes.NonSerialized) {<BR/> // Do not serialize<BR/> } else { <BR/> // Serialize<BR/> }<BR/> }<BR/>}Emir Unerhttps://www.blogger.com/profile/03360909344575293225noreply@blogger.com