public void GetSurrogate_ChainsToNextSelector() { var c = new StreamingContext(); var s1 = new SurrogateSelector(); var s2 = new SurrogateSelector(); s2.ChainSelector(s1); ISurrogateSelector selector; Assert.Null(s1.GetSurrogate(typeof(NonSerializablePair <int, string>), c, out selector)); Assert.Same(s1, selector); s1.AddSurrogate(typeof(NonSerializablePair <int, string>), c, new NonSerializablePairSurrogate()); Assert.NotNull(s1.GetSurrogate(typeof(NonSerializablePair <int, string>), c, out selector)); Assert.Same(s1, selector); Assert.NotNull(s2.GetSurrogate(typeof(NonSerializablePair <int, string>), c, out selector)); Assert.Same(s1, selector); s2.AddSurrogate(typeof(NonSerializablePair <int, string>), c, new NonSerializablePairSurrogate()); Assert.NotNull(s2.GetSurrogate(typeof(NonSerializablePair <int, string>), c, out selector)); Assert.Same(s2, selector); s2.RemoveSurrogate(typeof(NonSerializablePair <int, string>), c); Assert.NotNull(s2.GetSurrogate(typeof(NonSerializablePair <int, string>), c, out selector)); Assert.Same(s1, selector); s1.RemoveSurrogate(typeof(NonSerializablePair <int, string>), c); Assert.Null(s2.GetSurrogate(typeof(NonSerializablePair <int, string>), c, out selector)); Assert.Same(s1, selector); }
public static bool CheckForSurrogate(Type T) { ISurrogateSelector sur; BinaryFormatter bf = new BinaryFormatter() { TypeFormat = System.Runtime.Serialization.Formatters.FormatterTypeStyle.TypesWhenNeeded }; ISerializationSurrogate s = selector.GetSurrogate(T, sc, out sur); return(s != null || T.IsSerializable); }
public void GetSurrogate_InvalidArguments_ThrowExceptions() { var s = new SurrogateSelector(); var c = new StreamingContext(); ISurrogateSelector selector; AssertExtensions.Throws <ArgumentNullException>("type", () => s.GetSurrogate(null, c, out selector)); }
public ISerializationSurrogate GetSurrogate(Type type, StreamingContext context, out ISurrogateSelector selector) { if (!type.IsSerializable) { selector = this; return(new UnattributedTypeSerializationSurrogate()); } return(innerSelector.GetSurrogate(type, context, out selector)); }
public ISerializationSurrogate GetSurrogate(Type type, StreamingContext context, out ISurrogateSelector selector) { try { return(surrogate.GetSurrogate(type, context, out selector)); } catch { selector = this; return(null); } }
//Gets all the serializable members of an object and return an enumarable collection. private IEnumerable <SerializationEntry> GetMemberInfo(object objectToSerialize, Type objectToSerializeType, FormatterConverter converter) { ISurrogateSelector selector1 = null; ISerializationSurrogate serializationSurrogate = null; SerializationInfo info = null; Type objectType = objectToSerializeType; //If the passed object is null, break the iteration. if (objectToSerialize == null) { yield break; } //Get the serialization surrogate if (SurrogateSelector == null) { throw new NullReferenceException("An error occurred serializing an object. The SurrogateSelector property may not be null."); } serializationSurrogate = SurrogateSelector.GetSurrogate(objectType, Context, out selector1); if (serializationSurrogate == null) { throw new NullReferenceException(string.Format("An error occurred serializing an object. A surrogate was not found for type {0}", objectType.Name)); } //Use the surrogate to get the members. info = new SerializationInfo(objectType, converter); //Get the data from the surrogate. if (!objectType.IsPrimitive) { serializationSurrogate.GetObjectData(objectToSerialize, info, Context); } //Create the custom entries collection by copying all the members foreach (System.Runtime.Serialization.SerializationEntry member in info) { //SerializationEntry entry = new SerializationEntry(member.Name, member.ObjectType, member.Value); //yield return will return the entry now and return to this point when //the enclosing loop (the one that contains the call to this method) //request the next item. //yield return entry; yield return(member); } }
public static DiagramSerialize GetSurrogate(object graph, SurrogateSelector selector) { //Retrieve the surrogate reference //Sub classes may have added other surrogates //Start with the type of the deserialized data Type type = graph.GetType().UnderlyingSystemType; ISurrogateSelector iselector = (ISurrogateSelector)selector; DiagramSerialize surrogate = null; //Walk through the types until we find a surrogate while (type != typeof(System.Windows.Forms.UserControl)) { surrogate = (DiagramSerialize)selector.GetSurrogate(type, new StreamingContext(StreamingContextStates.All), out iselector); if (surrogate != null) { break; } type = type.BaseType; //return the base type and try again } return(surrogate); }
public bool runTest() { Console.WriteLine(s_strTFPath + " " + s_strTFName + " , for " + s_strClassMethod + " , Source ver " + s_strDtTmVer); int iCountErrors = 0; int iCountTestcases = 0; String strLoc = "Loc_000oo"; try { do { SurrogateSelector surrSelect1, surrSelect2, surrSelect3, surrSelect4, surrSelect5; TestSerializationClass testclass; TestSerializationSurrogate testss1, testss2, testss3, testss4, testss5; StreamingContext context = new StreamingContext(StreamingContextStates.CrossMachine); ISurrogateSelector ss; strLoc = "Loc_100aa"; surrSelect1 = new SurrogateSelector(); iCountTestcases++; try { surrSelect1.GetSurrogate(null, context, out ss); iCountErrors++; printerr("Error_100bb!! Expected ArgumentException not thrown"); } catch (ArgumentException) {} catch (Exception exc) { iCountErrors++; printerr("Error_100dd!! Incorrect Exception thrown : " + exc.ToString()); } strLoc = "Loc_300aa"; surrSelect1 = new SurrogateSelector(); testss1 = new TestSerializationSurrogate(); testclass = new TestSerializationClass(); surrSelect1.AddSurrogate(testclass.GetType(), context, testss1); iCountTestcases++; if (!surrSelect1.GetSurrogate(testclass.GetType(), context, out ss).Equals(testss1)) { iCountErrors++; printerr("Error_300bb! Incorrect ISerializationSurrogate returned"); } strLoc = "Loc_350aa"; testss1 = new TestSerializationSurrogate(); testss2 = new TestSerializationSurrogate(); testss3 = new TestSerializationSurrogate(); surrSelect1 = new SurrogateSelector(); surrSelect1.AddSurrogate(typeof(Int64), context, testss2); surrSelect1.AddSurrogate(typeof(Int32), context, testss3); iCountTestcases++; if (!surrSelect1.GetSurrogate(typeof(Int64), context, out ss).Equals(testss2)) { iCountErrors++; printerr("Error_350cc! Incorrect ISerializationSurrgate returned"); } iCountTestcases++; if (!surrSelect1.GetSurrogate(typeof(Int32), context, out ss).Equals(testss3)) { iCountErrors++; printerr("Error_350dd! Incorrect IserializationSurrogate returned"); } strLoc = "Loc_370aa"; testss1 = new TestSerializationSurrogate(); testss2 = new TestSerializationSurrogate(); testss3 = new TestSerializationSurrogate(); surrSelect1 = new SurrogateSelector(); surrSelect1.AddSurrogate(typeof(Int64), context, testss2); surrSelect1.AddSurrogate(typeof(Int32), context, testss3); strLoc = "Loc_400aa"; surrSelect1 = new SurrogateSelector(); surrSelect2 = new SurrogateSelector(); surrSelect3 = new SurrogateSelector(); surrSelect4 = new SurrogateSelector(); surrSelect5 = new SurrogateSelector(); testss1 = new TestSerializationSurrogate(); testss2 = new TestSerializationSurrogate(); testss3 = new TestSerializationSurrogate(); testss4 = new TestSerializationSurrogate(); testss5 = new TestSerializationSurrogate(); surrSelect3.ChainSelector(surrSelect5); surrSelect3.ChainSelector(surrSelect4); surrSelect2.ChainSelector(surrSelect3); surrSelect1.ChainSelector(surrSelect2); surrSelect5.AddSurrogate(typeof(Single), context, testss5); iCountTestcases++; if (!(surrSelect1.GetSurrogate(typeof(Single), context, out ss).Equals(testss5))) { iCountErrors++; printerr("Error_400bb!! Did not find the surrogate through the chain"); } iCountTestcases++; if (!(surrSelect3.GetSurrogate(typeof(Single), context, out ss).Equals(testss5))) { iCountErrors++; printerr("Error_400cc!! Did not find the surrogate through the chain"); } iCountTestcases++; if (!(surrSelect4.GetSurrogate(typeof(Single), context, out ss).Equals(testss5))) { iCountErrors++; printerr("Error_400dd!! Did not find the surrogate through the chain"); } strLoc = "Loc_401aa"; surrSelect1.AddSurrogate(typeof(Guid), context, testss1); iCountTestcases++; if (!(surrSelect1.GetSurrogate(typeof(Guid), context, out ss).Equals(testss1))) { iCountErrors++; printerr("Error_401bb!! Did not find the surrogate through the chain"); } iCountTestcases++; if (surrSelect2.GetSurrogate(typeof(Guid), context, out ss) != null) { iCountErrors++; printerr("Error_401cc!! Found surrogate that is above in the chain"); } strLoc = "Loc_402aa"; surrSelect2.AddSurrogate(typeof(Decimal), context, testss2); iCountTestcases++; if (!(surrSelect1.GetSurrogate(typeof(Decimal), context, out ss).Equals(testss2))) { iCountErrors++; printerr("Error_402bb!! Didn't find surrogate of child"); } strLoc = "Loc_403aa"; surrSelect3.AddSurrogate(typeof(Int32), context, testss3); iCountTestcases++; if (!(surrSelect1.GetSurrogate(typeof(Int32), context, out ss).Equals(testss3))) { iCountErrors++; printerr("Error_403bb!! Didn't find surrogate of child"); } strLoc = "Loc_404aa"; surrSelect4.AddSurrogate(typeof(Exception), context, testss4); iCountTestcases++; if (!(surrSelect1.GetSurrogate(typeof(Exception), context, out ss).Equals(testss4))) { iCountErrors++; printerr("Error_404bb!! Didn't find surrogate of child"); } strLoc = "Loc_405aa"; if (surrSelect1.GetSurrogate(typeof(Int16), context, out ss) != null) { iCountErrors++; printerr("Error_405bb!! Found surrogate not in chain"); } } while (false); } catch (Exception exc_general) { ++iCountErrors; Console.WriteLine(s_strTFAbbrev + " : Error Err_8888yyy! strLoc==" + strLoc + ", exc_general==" + exc_general.ToString()); Console.WriteLine(exc_general.StackTrace); } if (iCountErrors == 0) { Console.WriteLine("paSs. " + s_strTFPath + " " + s_strTFName + " ,iCountTestcases==" + iCountTestcases); return(true); } else { Console.WriteLine("FAiL! " + s_strTFPath + " " + s_strTFName + " ,iCountErrors==" + iCountErrors + " , BugNums?: " + s_strActiveBugNums); return(false); } }
public bool runTest() { Console.WriteLine(s_strTFPath + " " + s_strTFName + " , for " + s_strClassMethod + " , Source ver " + s_strDtTmVer); int iCountErrors = 0; int iCountTestcases = 0; String strLoc = "Loc_000oo"; try { do { SurrogateSelector surrSelect1, surrSelect2, surrSelect3, surrSelect4, surrSelect5; TestSerializationSurrogate testss1, testss2, testss3, testss4, testss5; StreamingContext context = new StreamingContext(StreamingContextStates.CrossMachine); ISurrogateSelector ss; SurrogateSelector[] arrSelectors; Int32 iHowDeep; strLoc = "Loc_100aa"; surrSelect1 = new SurrogateSelector(); iCountTestcases++; try { surrSelect1.ChainSelector(null); iCountErrors++; printerr("Error_100bb!! Expected ArgumentException not thrown"); } catch (ArgumentException){} catch (Exception exc) { iCountErrors++; printerr("Error_100dd!! Incorrect Exception thrown : "+exc.ToString()); } strLoc = "Loc_300aa"; iCountTestcases++; surrSelect1 = new SurrogateSelector(); try { surrSelect1.ChainSelector(surrSelect1); }catch (SerializationException){ }catch(Exception ex){ iCountErrors++; Console.WriteLine("Err_432fs!! Incorrect Exception thrown : "+ex.ToString()); } if(surrSelect1.GetSurrogate(typeof(Int64), context, out ss) != null) { iCountErrors++; printerr("Error_2139d!!!!"); } strLoc = "Loc_350aa"; testss1= new TestSerializationSurrogate(); testss2 = new TestSerializationSurrogate(); testss3 = new TestSerializationSurrogate(); surrSelect1 = new SurrogateSelector(); surrSelect1.AddSurrogate(typeof(Int64), context, testss2); surrSelect1.AddSurrogate(typeof(Int32), context, testss3); iCountTestcases++; if(!surrSelect1.GetSurrogate(typeof(Int64), context, out ss).Equals(testss2)) { iCountErrors++; printerr("Error_350cc! Incorrect ISerializationSurrgate returned"); } iCountTestcases++; if(!surrSelect1.GetSurrogate(typeof(Int32), context, out ss).Equals(testss3)) { iCountErrors++; printerr("Error_350dd! Incorrect IserializationSurrogate returned"); } strLoc = "Loc_370aa"; testss1= new TestSerializationSurrogate(); testss2 = new TestSerializationSurrogate(); testss3 = new TestSerializationSurrogate(); surrSelect1 = new SurrogateSelector(); surrSelect1.AddSurrogate(typeof(Int64), context, testss2); surrSelect1.AddSurrogate(typeof(Int32), context, testss3); strLoc = "Loc_400aa"; surrSelect1 = new SurrogateSelector(); surrSelect2 = new SurrogateSelector(); surrSelect3 = new SurrogateSelector(); surrSelect4 = new SurrogateSelector(); surrSelect5 = new SurrogateSelector(); testss1 = new TestSerializationSurrogate(); testss2 = new TestSerializationSurrogate(); testss3 = new TestSerializationSurrogate(); testss4 = new TestSerializationSurrogate(); testss5 = new TestSerializationSurrogate(); surrSelect3.ChainSelector(surrSelect5); surrSelect3.ChainSelector(surrSelect4); surrSelect2.ChainSelector(surrSelect3); surrSelect1.ChainSelector(surrSelect2); surrSelect5.AddSurrogate(typeof(Single), context, testss5); iCountTestcases++; if(!(surrSelect1.GetSurrogate(typeof(Single), context, out ss).Equals(testss5))) { iCountErrors++; printerr("Error_400bb!! Did not find the surrogate through the chain"); } iCountTestcases++; if(!(surrSelect3.GetSurrogate(typeof(Single), context, out ss).Equals(testss5))) { iCountErrors++; printerr("Error_400cc!! Did not find the surrogate through the chain"); } iCountTestcases++; if(!(surrSelect4.GetSurrogate(typeof(Single), context, out ss).Equals(testss5))) { iCountErrors++; printerr("Error_400dd!! Did not find the surrogate through the chain"); } strLoc = "Loc_401aa"; surrSelect1.AddSurrogate(typeof(Guid), context, testss1); iCountTestcases++; if(!(surrSelect1.GetSurrogate(typeof(Guid), context, out ss).Equals(testss1))) { iCountErrors++; printerr("Error_401bb!! Did not find the surrogate through the chain"); } iCountTestcases++; if(surrSelect2.GetSurrogate(typeof(Guid), context, out ss) != null) { iCountErrors++; printerr("Error_401cc!! Found surrogate that is above in the chain"); } strLoc = "Loc_402aa"; surrSelect2.AddSurrogate(typeof(Decimal), context, testss2); iCountTestcases++; if(!(surrSelect1.GetSurrogate(typeof(Decimal), context, out ss).Equals(testss2))) { iCountErrors++; printerr("Error_402bb!! Didn't find surrogate of child"); } strLoc = "Loc_403aa"; surrSelect3.AddSurrogate(typeof(Int32), context, testss3); iCountTestcases++; if(!(surrSelect1.GetSurrogate(typeof(Int32), context, out ss).Equals(testss3))) { iCountErrors++; printerr("Error_403bb!! Didn't find surrogate of child"); } strLoc = "Loc_404aa"; surrSelect4.AddSurrogate(typeof(Exception), context, testss4); iCountTestcases++; if(!(surrSelect1.GetSurrogate(typeof(Exception), context, out ss).Equals(testss4))) { iCountErrors++; printerr("Error_404bb!! Didn't find surrogate of child"); } strLoc = "Loc_405aa"; if(surrSelect1.GetSurrogate(typeof(Int16), context, out ss) != null) { iCountErrors++; printerr("Error_405bb!! Found surrogate not in chain"); } iHowDeep = 10; arrSelectors = new SurrogateSelector[iHowDeep]; arrSelectors[0] = new SurrogateSelector(); for(int i=1;i<iHowDeep;i++){ arrSelectors[i] = new SurrogateSelector(); arrSelectors[0].ChainSelector(arrSelectors[i]); } iCountTestcases++; if(arrSelectors[0].GetNextSelector() != arrSelectors[iHowDeep-1]) { iCountErrors++; printerr("Error_03842vdsd! Incorrect ISerializationSurrgate returned"); } for(int i=iHowDeep-1;i>1;i--){ iCountTestcases++; if(arrSelectors[i].GetNextSelector() != arrSelectors[i-1]){ iCountErrors++; printerr("Err_74239cd_" + i + "! Incorrect ISerializationSurrgate returned"); } } iCountTestcases++; if(arrSelectors[1].GetNextSelector() != null) { iCountErrors++; printerr("Error_0483vf! Incorrect ISerializationSurrgate returned"); } try { iCountTestcases++; arrSelectors[2].ChainSelector(arrSelectors[8]); context = new StreamingContext(StreamingContextStates.All); testss1 = (TestSerializationSurrogate)arrSelectors[0].GetSurrogate(typeof(Int64), context, out ss); iCountErrors++; Console.WriteLine("Err_74239cd!! Expected Exception not thrown"); }catch(ArgumentException){ }catch(Exception ex){ iCountErrors++; Console.WriteLine("Err_652df!! Incorrect Exception thrown : "+ex.ToString()); } } while (false); } catch (Exception exc_general ) { ++iCountErrors; Console.WriteLine (s_strTFAbbrev + " : Error Err_8888yyy! strLoc=="+ strLoc +", exc_general=="+exc_general.ToString()); Console.WriteLine(exc_general.StackTrace); } if ( iCountErrors == 0 ) { Console.WriteLine( "paSs. "+s_strTFPath +" "+s_strTFName+" ,iCountTestcases=="+iCountTestcases); return true; } else { Console.WriteLine("FAiL! "+s_strTFPath+" "+s_strTFName+" ,iCountErrors=="+iCountErrors+" , BugNums?: "+s_strActiveBugNums ); return false; } }
//Reads an object from the XML and initializes the object. private object InitializeObject(XmlTextReader reader, FormatterConverter converter) { Type actualType; int id; //Check if a type or ref attribute is present if (!reader.HasAttributes) { throw new SerializationException("A non-primitive element was found without attributes."); } //Check for a ref attribute string reference = reader.GetAttribute("ref"); //, "http://www.w3.org/2001/XMLSchema-instance"); //References require a previously deserialized object if (reference != null) { if (!int.TryParse(reference, out id)) { throw new SerializationException("Non numeric reference id found."); } object existing = null; if (!_idObjects.TryGetValue(id, out existing)) { throw new SerializationException(string.Format("An object reference with id {0} was not previously deserialized.", reference)); } return(existing); } //Get the type name string actualTypeName = reader.GetAttribute("type"); // "http://www.w3.org/2001/XMLSchema-instance"); actualType = Binder.BindToType("", actualTypeName); //Get the id attribute string objectId = reader.GetAttribute("id"); //, "http://www.w3.org/2001/XMLSchema-instance"); //If the id is null then the object reference is null so return null if (objectId == null) { return(null); } //Convert to an integer value if (!int.TryParse(objectId, out id)) { throw new SerializationException("An object id could not be converted to an integer value."); } ISurrogateSelector selector1 = null; ISerializationSurrogate serializationSurrogate = null; SerializationInfo info = null; if (SurrogateSelector == null) { throw new NullReferenceException("An error occurred deserializing an object. The SurrogateSelector property may not be null."); } serializationSurrogate = SurrogateSelector.GetSurrogate(actualType, Context, out selector1); if (serializationSurrogate == null) { throw new NullReferenceException(string.Format("An error occurred deserializing an object. A surrogate was not found for type {0}", actualType.Name)); } //Use surrogate info = new SerializationInfo(actualType, converter); //Create a instance of the type, or use the existing object graph object initializedObject; if (Target == null) { initializedObject = FormatterServices.GetUninitializedObject(actualType); //Call the default constructor //Formatter could be expanded to use non default constructors at some later stage ConstructorInfo ci = actualType.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, System.Type.EmptyTypes, null); if (ci == null) { throw new SerializationException(string.Format("Type {0} must implement a parameterless constructor.", actualType.FullName)); } ci.Invoke(initializedObject, null); } else { initializedObject = Target; Target = null; //Reset target so that it cannot be used recursively } //Add the object to the list of id objects _idObjects.Add(id, initializedObject); //Determine if a list or collection IDictionary dictionary = null; IList list = null; int index = 0; //Read the first element reader.ReadStartElement(); while (reader.IsStartElement()) { //Determine type string typeName = reader.GetAttribute("type"); //, "http://www.w3.org/2001/XMLSchema-instance"); Type childType = Binder.BindToType("", typeName); //Get a key, if any string key = reader.GetAttribute("key"); //Check for a uri attribute string uri = reader.GetAttribute("uri"); //Check if a collection if (reader.Name == "Collection" && reader.IsStartElement()) { if (typeName == "IDictionary") { dictionary = initializedObject as IDictionary; list = null; } else if (typeName == "IList") { list = initializedObject as IList; dictionary = null; } reader.ReadStartElement(); } //Check for a resource entry else if (uri != null) { //Uris require a resource preloaded in the _resources collection ResourceEntry resourceEntry; if (!_resources.TryGetValue(uri, out resourceEntry)) { throw new SerializationException(string.Format("A resource with uri {0} was not found in the resources collection.", uri)); } info.AddValue(resourceEntry.Name, resourceEntry.Value); reader.Read(); } //Process all other elements else { object parsedObject = null; //Check if the value can be directly determined or that the type is a complex type. if (childType.IsPrimitive || childType == typeof(string) || childType.IsEnum || childType == typeof(DateTime) || childType == typeof(object)) { //Directly parse parsedObject = converter.Convert(reader.ReadString(), childType); } else { //Recurse down the object graph parsedObject = InitializeObject(reader, converter); } //Add to parent collection or add the key value pair to the info object for the serialization surrogate to use if (dictionary != null && key != null) { dictionary.Add(key, parsedObject); } else if (list != null) { list.Add(parsedObject); } else { info.AddValue(reader.Name, parsedObject); //Use info object } //Move to next element reader.Read(); //Read past collection if (reader.Name == "Collection" && !reader.IsStartElement()) { reader.Read(); } } } //Use the surrogate to populate the instance initializedObject = serializationSurrogate.SetObjectData(initializedObject, info, Context, SurrogateSelector); return(initializedObject); }
public bool runTest() { Console.WriteLine(s_strTFPath + " " + s_strTFName + " , for " + s_strClassMethod + " , Source ver " + s_strDtTmVer); int iCountErrors = 0; int iCountTestcases = 0; String strLoc = "Loc_000oo"; try { do { SurrogateSelector surrSelect1, surrSelect2, surrSelect3, surrSelect4, surrSelect5; TestSerializationClass testclass; TestSerializationSurrogate testss1, testss2, testss3, testss4, testss5; StreamingContext context = new StreamingContext(StreamingContextStates.CrossMachine); ISurrogateSelector ss; strLoc = "Loc_100aa"; surrSelect1 = new SurrogateSelector(); iCountTestcases++; try { surrSelect1.GetSurrogate(null, context, out ss); iCountErrors++; printerr("Error_100bb!! Expected ArgumentException not thrown"); } catch (ArgumentException){} catch (Exception exc) { iCountErrors++; printerr("Error_100dd!! Incorrect Exception thrown : "+exc.ToString()); } strLoc = "Loc_300aa"; surrSelect1 = new SurrogateSelector(); testss1= new TestSerializationSurrogate(); testclass = new TestSerializationClass(); surrSelect1.AddSurrogate(testclass.GetType(), context, testss1); iCountTestcases++; if(!surrSelect1.GetSurrogate(testclass.GetType(), context, out ss).Equals(testss1)) { iCountErrors++; printerr("Error_300bb! Incorrect ISerializationSurrogate returned"); } strLoc = "Loc_350aa"; testss1= new TestSerializationSurrogate(); testss2 = new TestSerializationSurrogate(); testss3 = new TestSerializationSurrogate(); surrSelect1 = new SurrogateSelector(); surrSelect1.AddSurrogate(typeof(Int64), context, testss2); surrSelect1.AddSurrogate(typeof(Int32), context, testss3); iCountTestcases++; if(!surrSelect1.GetSurrogate(typeof(Int64), context, out ss).Equals(testss2)) { iCountErrors++; printerr("Error_350cc! Incorrect ISerializationSurrgate returned"); } iCountTestcases++; if(!surrSelect1.GetSurrogate(typeof(Int32), context, out ss).Equals(testss3)) { iCountErrors++; printerr("Error_350dd! Incorrect IserializationSurrogate returned"); } strLoc = "Loc_370aa"; testss1= new TestSerializationSurrogate(); testss2 = new TestSerializationSurrogate(); testss3 = new TestSerializationSurrogate(); surrSelect1 = new SurrogateSelector(); surrSelect1.AddSurrogate(typeof(Int64), context, testss2); surrSelect1.AddSurrogate(typeof(Int32), context, testss3); strLoc = "Loc_400aa"; surrSelect1 = new SurrogateSelector(); surrSelect2 = new SurrogateSelector(); surrSelect3 = new SurrogateSelector(); surrSelect4 = new SurrogateSelector(); surrSelect5 = new SurrogateSelector(); testss1 = new TestSerializationSurrogate(); testss2 = new TestSerializationSurrogate(); testss3 = new TestSerializationSurrogate(); testss4 = new TestSerializationSurrogate(); testss5 = new TestSerializationSurrogate(); surrSelect3.ChainSelector(surrSelect5); surrSelect3.ChainSelector(surrSelect4); surrSelect2.ChainSelector(surrSelect3); surrSelect1.ChainSelector(surrSelect2); surrSelect5.AddSurrogate(typeof(Single), context, testss5); iCountTestcases++; if(!(surrSelect1.GetSurrogate(typeof(Single), context, out ss).Equals(testss5))) { iCountErrors++; printerr("Error_400bb!! Did not find the surrogate through the chain"); } iCountTestcases++; if(!(surrSelect3.GetSurrogate(typeof(Single), context, out ss).Equals(testss5))) { iCountErrors++; printerr("Error_400cc!! Did not find the surrogate through the chain"); } iCountTestcases++; if(!(surrSelect4.GetSurrogate(typeof(Single), context, out ss).Equals(testss5))) { iCountErrors++; printerr("Error_400dd!! Did not find the surrogate through the chain"); } strLoc = "Loc_401aa"; surrSelect1.AddSurrogate(typeof(Guid), context, testss1); iCountTestcases++; if(!(surrSelect1.GetSurrogate(typeof(Guid), context, out ss).Equals(testss1))) { iCountErrors++; printerr("Error_401bb!! Did not find the surrogate through the chain"); } iCountTestcases++; if(surrSelect2.GetSurrogate(typeof(Guid), context, out ss) != null) { iCountErrors++; printerr("Error_401cc!! Found surrogate that is above in the chain"); } strLoc = "Loc_402aa"; surrSelect2.AddSurrogate(typeof(Decimal), context, testss2); iCountTestcases++; if(!(surrSelect1.GetSurrogate(typeof(Decimal), context, out ss).Equals(testss2))) { iCountErrors++; printerr("Error_402bb!! Didn't find surrogate of child"); } strLoc = "Loc_403aa"; surrSelect3.AddSurrogate(typeof(Int32), context, testss3); iCountTestcases++; if(!(surrSelect1.GetSurrogate(typeof(Int32), context, out ss).Equals(testss3))) { iCountErrors++; printerr("Error_403bb!! Didn't find surrogate of child"); } strLoc = "Loc_404aa"; surrSelect4.AddSurrogate(typeof(Exception), context, testss4); iCountTestcases++; if(!(surrSelect1.GetSurrogate(typeof(Exception), context, out ss).Equals(testss4))) { iCountErrors++; printerr("Error_404bb!! Didn't find surrogate of child"); } strLoc = "Loc_405aa"; if(surrSelect1.GetSurrogate(typeof(Int16), context, out ss) != null) { iCountErrors++; printerr("Error_405bb!! Found surrogate not in chain"); } } while (false); } catch (Exception exc_general ) { ++iCountErrors; Console.WriteLine (s_strTFAbbrev + " : Error Err_8888yyy! strLoc=="+ strLoc +", exc_general=="+exc_general.ToString()); Console.WriteLine(exc_general.StackTrace); } if ( iCountErrors == 0 ) { Console.WriteLine( "paSs. "+s_strTFPath +" "+s_strTFName+" ,iCountTestcases=="+iCountTestcases); return true; } else { Console.WriteLine("FAiL! "+s_strTFPath+" "+s_strTFName+" ,iCountErrors=="+iCountErrors+" , BugNums?: "+s_strActiveBugNums ); return false; } }
public bool runTest() { Console.WriteLine(s_strTFPath + " " + s_strTFName + " , for " + s_strClassMethod + " , Source ver " + s_strDtTmVer); int iCountErrors = 0; int iCountTestcases = 0; String strLoc = "Loc_000oo"; try { do { SurrogateSelector surrSelect1, surrSelect2, surrSelect3, surrSelect4, surrSelect5; TestSerializationSurrogate testss1, testss2, testss3, testss4, testss5; StreamingContext context = new StreamingContext(StreamingContextStates.CrossMachine); ISurrogateSelector ss; SurrogateSelector[] arrSelectors; Int32 iHowDeep; strLoc = "Loc_100aa"; surrSelect1 = new SurrogateSelector(); iCountTestcases++; try { surrSelect1.ChainSelector(null); iCountErrors++; printerr("Error_100bb!! Expected ArgumentException not thrown"); } catch (ArgumentException) {} catch (Exception exc) { iCountErrors++; printerr("Error_100dd!! Incorrect Exception thrown : " + exc.ToString()); } strLoc = "Loc_300aa"; iCountTestcases++; surrSelect1 = new SurrogateSelector(); try { surrSelect1.ChainSelector(surrSelect1); }catch (SerializationException) { }catch (Exception ex) { iCountErrors++; Console.WriteLine("Err_432fs!! Incorrect Exception thrown : " + ex.ToString()); } if (surrSelect1.GetSurrogate(typeof(Int64), context, out ss) != null) { iCountErrors++; printerr("Error_2139d!!!!"); } strLoc = "Loc_350aa"; testss1 = new TestSerializationSurrogate(); testss2 = new TestSerializationSurrogate(); testss3 = new TestSerializationSurrogate(); surrSelect1 = new SurrogateSelector(); surrSelect1.AddSurrogate(typeof(Int64), context, testss2); surrSelect1.AddSurrogate(typeof(Int32), context, testss3); iCountTestcases++; if (!surrSelect1.GetSurrogate(typeof(Int64), context, out ss).Equals(testss2)) { iCountErrors++; printerr("Error_350cc! Incorrect ISerializationSurrgate returned"); } iCountTestcases++; if (!surrSelect1.GetSurrogate(typeof(Int32), context, out ss).Equals(testss3)) { iCountErrors++; printerr("Error_350dd! Incorrect IserializationSurrogate returned"); } strLoc = "Loc_370aa"; testss1 = new TestSerializationSurrogate(); testss2 = new TestSerializationSurrogate(); testss3 = new TestSerializationSurrogate(); surrSelect1 = new SurrogateSelector(); surrSelect1.AddSurrogate(typeof(Int64), context, testss2); surrSelect1.AddSurrogate(typeof(Int32), context, testss3); strLoc = "Loc_400aa"; surrSelect1 = new SurrogateSelector(); surrSelect2 = new SurrogateSelector(); surrSelect3 = new SurrogateSelector(); surrSelect4 = new SurrogateSelector(); surrSelect5 = new SurrogateSelector(); testss1 = new TestSerializationSurrogate(); testss2 = new TestSerializationSurrogate(); testss3 = new TestSerializationSurrogate(); testss4 = new TestSerializationSurrogate(); testss5 = new TestSerializationSurrogate(); surrSelect3.ChainSelector(surrSelect5); surrSelect3.ChainSelector(surrSelect4); surrSelect2.ChainSelector(surrSelect3); surrSelect1.ChainSelector(surrSelect2); surrSelect5.AddSurrogate(typeof(Single), context, testss5); iCountTestcases++; if (!(surrSelect1.GetSurrogate(typeof(Single), context, out ss).Equals(testss5))) { iCountErrors++; printerr("Error_400bb!! Did not find the surrogate through the chain"); } iCountTestcases++; if (!(surrSelect3.GetSurrogate(typeof(Single), context, out ss).Equals(testss5))) { iCountErrors++; printerr("Error_400cc!! Did not find the surrogate through the chain"); } iCountTestcases++; if (!(surrSelect4.GetSurrogate(typeof(Single), context, out ss).Equals(testss5))) { iCountErrors++; printerr("Error_400dd!! Did not find the surrogate through the chain"); } strLoc = "Loc_401aa"; surrSelect1.AddSurrogate(typeof(Guid), context, testss1); iCountTestcases++; if (!(surrSelect1.GetSurrogate(typeof(Guid), context, out ss).Equals(testss1))) { iCountErrors++; printerr("Error_401bb!! Did not find the surrogate through the chain"); } iCountTestcases++; if (surrSelect2.GetSurrogate(typeof(Guid), context, out ss) != null) { iCountErrors++; printerr("Error_401cc!! Found surrogate that is above in the chain"); } strLoc = "Loc_402aa"; surrSelect2.AddSurrogate(typeof(Decimal), context, testss2); iCountTestcases++; if (!(surrSelect1.GetSurrogate(typeof(Decimal), context, out ss).Equals(testss2))) { iCountErrors++; printerr("Error_402bb!! Didn't find surrogate of child"); } strLoc = "Loc_403aa"; surrSelect3.AddSurrogate(typeof(Int32), context, testss3); iCountTestcases++; if (!(surrSelect1.GetSurrogate(typeof(Int32), context, out ss).Equals(testss3))) { iCountErrors++; printerr("Error_403bb!! Didn't find surrogate of child"); } strLoc = "Loc_404aa"; surrSelect4.AddSurrogate(typeof(Exception), context, testss4); iCountTestcases++; if (!(surrSelect1.GetSurrogate(typeof(Exception), context, out ss).Equals(testss4))) { iCountErrors++; printerr("Error_404bb!! Didn't find surrogate of child"); } strLoc = "Loc_405aa"; if (surrSelect1.GetSurrogate(typeof(Int16), context, out ss) != null) { iCountErrors++; printerr("Error_405bb!! Found surrogate not in chain"); } iHowDeep = 10; arrSelectors = new SurrogateSelector[iHowDeep]; arrSelectors[0] = new SurrogateSelector(); for (int i = 1; i < iHowDeep; i++) { arrSelectors[i] = new SurrogateSelector(); arrSelectors[0].ChainSelector(arrSelectors[i]); } iCountTestcases++; if (arrSelectors[0].GetNextSelector() != arrSelectors[iHowDeep - 1]) { iCountErrors++; printerr("Error_03842vdsd! Incorrect ISerializationSurrgate returned"); } for (int i = iHowDeep - 1; i > 1; i--) { iCountTestcases++; if (arrSelectors[i].GetNextSelector() != arrSelectors[i - 1]) { iCountErrors++; printerr("Err_74239cd_" + i + "! Incorrect ISerializationSurrgate returned"); } } iCountTestcases++; if (arrSelectors[1].GetNextSelector() != null) { iCountErrors++; printerr("Error_0483vf! Incorrect ISerializationSurrgate returned"); } try { iCountTestcases++; arrSelectors[2].ChainSelector(arrSelectors[8]); context = new StreamingContext(StreamingContextStates.All); testss1 = (TestSerializationSurrogate)arrSelectors[0].GetSurrogate(typeof(Int64), context, out ss); iCountErrors++; Console.WriteLine("Err_74239cd!! Expected Exception not thrown"); }catch (ArgumentException) { }catch (Exception ex) { iCountErrors++; Console.WriteLine("Err_652df!! Incorrect Exception thrown : " + ex.ToString()); } } while (false); } catch (Exception exc_general) { ++iCountErrors; Console.WriteLine(s_strTFAbbrev + " : Error Err_8888yyy! strLoc==" + strLoc + ", exc_general==" + exc_general.ToString()); Console.WriteLine(exc_general.StackTrace); } if (iCountErrors == 0) { Console.WriteLine("paSs. " + s_strTFPath + " " + s_strTFName + " ,iCountTestcases==" + iCountTestcases); return(true); } else { Console.WriteLine("FAiL! " + s_strTFPath + " " + s_strTFName + " ,iCountErrors==" + iCountErrors + " , BugNums?: " + s_strActiveBugNums); return(false); } }
public bool runTest() { Console.WriteLine(s_strTFPath + " " + s_strTFName + " , for " + s_strClassMethod + " , Source ver " + s_strDtTmVer); int iCountErrors = 0; int iCountTestcases = 0; String strLoc = "Loc_000oo"; try { do { SurrogateSelector surrSelect; SurrogateSelector surrSelect_next; TestSerializationClass testclass; TestSerializationSurrogate testss, testss1, testss2; StreamingContext context = new StreamingContext(StreamingContextStates.CrossMachine); ISurrogateSelector ss; SurrogateSelector[] arrSelectors; Int32 iHowDeep; strLoc = "Loc_100aa"; surrSelect = new SurrogateSelector(); iCountTestcases++; try { surrSelect.AddSurrogate(this.GetType(), context, null); iCountErrors++; printerr("Error_100bb! Expected exception not thrown"); } catch (ArgumentException) {} catch (Exception exc) { iCountErrors++; printerr("Error_100dd! Incorrect Exception thrown: " + exc.ToString()); } strLoc = "Loc_200aa"; surrSelect = new SurrogateSelector(); testss = new TestSerializationSurrogate(); iCountTestcases++; try { surrSelect.AddSurrogate(null, context, testss); iCountErrors++; printerr("Error_200bb! Expected exception not thrown"); } catch (ArgumentException) {} catch (Exception exc) { iCountErrors++; printerr("Error_200dd! Incorrect exception thrown: " + exc.ToString()); } strLoc = "Loc_300aa"; surrSelect = new SurrogateSelector(); testss = new TestSerializationSurrogate(); testclass = new TestSerializationClass(); surrSelect.AddSurrogate(testclass.GetType(), context, testss); iCountTestcases++; if (!surrSelect.GetSurrogate(testclass.GetType(), context, out ss).Equals(testss)) { iCountErrors++; printerr("Error_300bb! Incorrect ISerializationSurrogate returned"); } strLoc = "Loc_350aa"; testss = new TestSerializationSurrogate(); testss1 = new TestSerializationSurrogate(); testss2 = new TestSerializationSurrogate(); surrSelect = new SurrogateSelector(); surrSelect.AddSurrogate(typeof(Decimal), context, testss); surrSelect.AddSurrogate(typeof(Int64), context, testss1); surrSelect.AddSurrogate(typeof(Int32), context, testss2); iCountTestcases++; if (!surrSelect.GetSurrogate(typeof(Decimal), context, out ss).Equals(testss)) { iCountErrors++; printerr("Error_350bb! Incorrect ISerializationSurrogate returned"); } iCountTestcases++; if (surrSelect != ss) { iCountErrors++; printerr("Error_0453cd! Incorrect ISurrogateSelector returned"); } iCountTestcases++; if (!surrSelect.GetSurrogate(typeof(Int64), context, out ss).Equals(testss1)) { iCountErrors++; printerr("Error_350cc! Incorrect ISerializationSurrgate returned"); } iCountTestcases++; if (!surrSelect.GetSurrogate(typeof(Int32), context, out ss).Equals(testss2)) { iCountErrors++; printerr("Error_350dd! Incorrect IserializationSurrogate returned"); } strLoc = "Loc_370aa"; testss = new TestSerializationSurrogate(); testss1 = new TestSerializationSurrogate(); testss2 = new TestSerializationSurrogate(); surrSelect = new SurrogateSelector(); surrSelect.AddSurrogate(typeof(Decimal), context, testss); surrSelect.AddSurrogate(typeof(Int64), context, testss1); surrSelect.AddSurrogate(typeof(Int32), context, testss2); iCountTestcases++; if (surrSelect.GetSurrogate(typeof(Int16), context, out ss) != null) { iCountErrors++; printerr("Error_370bb! Type not in surrogateselector was actually found???"); } strLoc = "Loc_400aa"; surrSelect = new SurrogateSelector(); testss = new TestSerializationSurrogate(); testclass = new TestSerializationClass(); surrSelect.AddSurrogate(testclass.GetType(), context, testss); try { surrSelect.AddSurrogate(testclass.GetType(), context, new TestSerializationSurrogate()); iCountErrors++; printerr("Error_400bb! Expected Exception not thrown"); }catch (ArgumentException) { }catch (Exception exc) { iCountErrors++; printerr("Error_400cc! Incorrect Exception thrown : " + exc.ToString()); } surrSelect = new SurrogateSelector(); surrSelect_next = new SurrogateSelector(); surrSelect.ChainSelector(surrSelect_next); testss = new TestSerializationSurrogate(); context = new StreamingContext(StreamingContextStates.All); surrSelect_next.AddSurrogate(typeof(Decimal), context, testss); iCountTestcases++; if (!surrSelect.GetSurrogate(typeof(Decimal), context, out ss).Equals(testss)) { iCountErrors++; printerr("Error_04582fd! Incorrect ISerializationSurrogate returned"); } iCountTestcases++; if (surrSelect == ss) { iCountErrors++; printerr("Error_0478523vdf! Incorrect ISurrogateSelector returned"); } iCountTestcases++; if (surrSelect_next != ss) { iCountErrors++; printerr("Error_934721cd! Incorrect ISurrogateSelector returned"); } iHowDeep = 100; arrSelectors = new SurrogateSelector[iHowDeep]; arrSelectors[0] = new SurrogateSelector(); for (int i = 1; i < iHowDeep; i++) { arrSelectors[i] = new SurrogateSelector(); arrSelectors[0].ChainSelector(arrSelectors[i]); } testss = new TestSerializationSurrogate(); context = new StreamingContext(StreamingContextStates.All); arrSelectors[1].AddSurrogate(typeof(Decimal), context, testss); iCountTestcases++; if (!arrSelectors[0].GetSurrogate(typeof(Decimal), context, out ss).Equals(testss)) { iCountErrors++; printerr("Error_743cd! Incorrect ISerializationSurrogate returned"); } iCountTestcases++; if (arrSelectors[1] != ss) { iCountErrors++; printerr("Error_10573cd! Incorrect ISurrogateSelector returned"); } arrSelectors[0].AddSurrogate(typeof(Decimal), context, testss); iCountTestcases++; if (!arrSelectors[0].GetSurrogate(typeof(Decimal), context, out ss).Equals(testss)) { iCountErrors++; printerr("Error_0472ds! Incorrect ISerializationSurrogate returned"); } iCountTestcases++; if (arrSelectors[0] != ss) { iCountErrors++; printerr("Error_0342dfg! Incorrect ISurrogateSelector returned"); } } while (false); } catch (Exception exc_general) { ++iCountErrors; Console.WriteLine(s_strTFAbbrev + " : Error Err_8888yyy! strLoc==" + strLoc + ", exc_general==" + exc_general.ToString()); Console.WriteLine(exc_general.StackTrace); } if (iCountErrors == 0) { Console.WriteLine("paSs. " + s_strTFPath + " " + s_strTFName + " ,iCountTestcases==" + iCountTestcases); return(true); } else { Console.WriteLine("FAiL! " + s_strTFPath + " " + s_strTFName + " ,iCountErrors==" + iCountErrors + " , BugNums?: " + s_strActiveBugNums); return(false); } }
public bool runTest() { Console.WriteLine(s_strTFPath + " " + s_strTFName + " , for " + s_strClassMethod + " , Source ver " + s_strDtTmVer); int iCountErrors = 0; int iCountTestcases = 0; String strLoc = "Loc_000oo"; try { do { SurrogateSelector surrSelect; SurrogateSelector surrSelect_next; TestSerializationClass testclass; TestSerializationSurrogate testss, testss1, testss2; StreamingContext context = new StreamingContext(StreamingContextStates.CrossMachine); ISurrogateSelector ss; SurrogateSelector[] arrSelectors; Int32 iHowDeep; strLoc = "Loc_100aa"; surrSelect = new SurrogateSelector(); iCountTestcases++; try { surrSelect.AddSurrogate(this.GetType(), context, null); iCountErrors++; printerr("Error_100bb! Expected exception not thrown"); } catch (ArgumentException){} catch (Exception exc) { iCountErrors++; printerr("Error_100dd! Incorrect Exception thrown: "+exc.ToString()); } strLoc = "Loc_200aa"; surrSelect = new SurrogateSelector(); testss = new TestSerializationSurrogate(); iCountTestcases++; try { surrSelect.AddSurrogate(null, context, testss); iCountErrors++; printerr("Error_200bb! Expected exception not thrown"); } catch (ArgumentException){} catch (Exception exc) { iCountErrors++; printerr("Error_200dd! Incorrect exception thrown: "+exc.ToString()); } strLoc = "Loc_300aa"; surrSelect = new SurrogateSelector(); testss = new TestSerializationSurrogate(); testclass = new TestSerializationClass(); surrSelect.AddSurrogate(testclass.GetType(), context, testss); iCountTestcases++; if(!surrSelect.GetSurrogate(testclass.GetType(), context, out ss).Equals(testss)) { iCountErrors++; printerr("Error_300bb! Incorrect ISerializationSurrogate returned"); } strLoc = "Loc_350aa"; testss = new TestSerializationSurrogate(); testss1 = new TestSerializationSurrogate(); testss2 = new TestSerializationSurrogate(); surrSelect = new SurrogateSelector(); surrSelect.AddSurrogate(typeof(Decimal), context, testss); surrSelect.AddSurrogate(typeof(Int64),context, testss1); surrSelect.AddSurrogate(typeof(Int32),context, testss2); iCountTestcases++; if(!surrSelect.GetSurrogate(typeof(Decimal), context, out ss).Equals(testss)) { iCountErrors++; printerr("Error_350bb! Incorrect ISerializationSurrogate returned"); } iCountTestcases++; if(surrSelect != ss) { iCountErrors++; printerr("Error_0453cd! Incorrect ISurrogateSelector returned"); } iCountTestcases++; if(!surrSelect.GetSurrogate(typeof(Int64), context, out ss).Equals(testss1)) { iCountErrors++; printerr("Error_350cc! Incorrect ISerializationSurrgate returned"); } iCountTestcases++; if(!surrSelect.GetSurrogate(typeof(Int32), context, out ss).Equals(testss2)) { iCountErrors++; printerr("Error_350dd! Incorrect IserializationSurrogate returned"); } strLoc = "Loc_370aa"; testss = new TestSerializationSurrogate(); testss1 = new TestSerializationSurrogate(); testss2 = new TestSerializationSurrogate(); surrSelect = new SurrogateSelector(); surrSelect.AddSurrogate(typeof(Decimal), context, testss); surrSelect.AddSurrogate(typeof(Int64),context, testss1); surrSelect.AddSurrogate(typeof(Int32),context, testss2); iCountTestcases++; if(surrSelect.GetSurrogate(typeof(Int16), context, out ss) != null) { iCountErrors++; printerr("Error_370bb! Type not in surrogateselector was actually found???"); } strLoc = "Loc_400aa"; surrSelect = new SurrogateSelector(); testss = new TestSerializationSurrogate(); testclass = new TestSerializationClass(); surrSelect.AddSurrogate(testclass.GetType(),context, testss); try { surrSelect.AddSurrogate(testclass.GetType(),context, new TestSerializationSurrogate()); iCountErrors++; printerr("Error_400bb! Expected Exception not thrown"); }catch (ArgumentException){ }catch (Exception exc){ iCountErrors++; printerr("Error_400cc! Incorrect Exception thrown : "+exc.ToString()); } surrSelect = new SurrogateSelector(); surrSelect_next = new SurrogateSelector(); surrSelect.ChainSelector(surrSelect_next); testss = new TestSerializationSurrogate(); context = new StreamingContext(StreamingContextStates.All); surrSelect_next.AddSurrogate(typeof(Decimal), context, testss); iCountTestcases++; if(!surrSelect.GetSurrogate(typeof(Decimal), context, out ss).Equals(testss)) { iCountErrors++; printerr("Error_04582fd! Incorrect ISerializationSurrogate returned"); } iCountTestcases++; if(surrSelect == ss) { iCountErrors++; printerr("Error_0478523vdf! Incorrect ISurrogateSelector returned"); } iCountTestcases++; if(surrSelect_next != ss) { iCountErrors++; printerr("Error_934721cd! Incorrect ISurrogateSelector returned"); } iHowDeep = 100; arrSelectors = new SurrogateSelector[iHowDeep]; arrSelectors[0] = new SurrogateSelector(); for(int i=1;i<iHowDeep;i++){ arrSelectors[i] = new SurrogateSelector(); arrSelectors[0].ChainSelector(arrSelectors[i]); } testss = new TestSerializationSurrogate(); context = new StreamingContext(StreamingContextStates.All); arrSelectors[1].AddSurrogate(typeof(Decimal), context, testss); iCountTestcases++; if(!arrSelectors[0].GetSurrogate(typeof(Decimal), context, out ss).Equals(testss)) { iCountErrors++; printerr("Error_743cd! Incorrect ISerializationSurrogate returned"); } iCountTestcases++; if(arrSelectors[1] != ss) { iCountErrors++; printerr("Error_10573cd! Incorrect ISurrogateSelector returned"); } arrSelectors[0].AddSurrogate(typeof(Decimal), context, testss); iCountTestcases++; if(!arrSelectors[0].GetSurrogate(typeof(Decimal), context, out ss).Equals(testss)) { iCountErrors++; printerr("Error_0472ds! Incorrect ISerializationSurrogate returned"); } iCountTestcases++; if(arrSelectors[0] != ss) { iCountErrors++; printerr("Error_0342dfg! Incorrect ISurrogateSelector returned"); } } while (false); } catch (Exception exc_general ) { ++iCountErrors; Console.WriteLine (s_strTFAbbrev + " : Error Err_8888yyy! strLoc=="+ strLoc +", exc_general=="+exc_general.ToString()); Console.WriteLine(exc_general.StackTrace); } if ( iCountErrors == 0 ) { Console.WriteLine( "paSs. "+s_strTFPath +" "+s_strTFName+" ,iCountTestcases=="+iCountTestcases); return true; } else { Console.WriteLine("FAiL! "+s_strTFPath+" "+s_strTFName+" ,iCountErrors=="+iCountErrors+" , BugNums?: "+s_strActiveBugNums ); return false; } }
/// <summary> /// Reads an object from the XML and initializes the object. /// </summary> /// <param name="reader">The XmlReader to read from.</param> /// <param name="converter">The converter used to parse the values from the XML.</param> /// <param name="objectType">The type of the object to create.</param> /// <returns>The recreated object.</returns> private object InitializeObject(XmlTextReader reader, FormatterConverter converter, Type objectType) { Type actualType; ISurrogateSelector selector1 = null; ISerializationSurrogate serializationSurrogate = null; SerializationInfo info = null; object initializedObject = null; // check if a type attribute is present if (reader.HasAttributes) { // if so, get the type string actualTypeName = reader.GetAttribute("type", "http://www.w3.org/2001/XMLSchema-instance"); actualType = Binder.BindToType("", actualTypeName); } else { // passed type is actual type. actualType = objectType; } // check whether a surrogate should be used, iserializable is implemented or reflection is needed. if ((SurrogateSelector != null) && ((serializationSurrogate = SurrogateSelector.GetSurrogate(actualType, Context, out selector1)) != null)) { // use surrogate info = new SerializationInfo(actualType, converter); if (!actualType.IsPrimitive) { // create a instance of the type. initializedObject = FormatterServices.GetUninitializedObject(actualType); // read the first element reader.ReadStartElement(); while (reader.IsStartElement()) { // determine type string typeName = reader.GetAttribute("type", "http://www.w3.org/2001/XMLSchema-instance"); Type type = Binder.BindToType("", typeName); // using ISerializable info.AddValue(reader.Name, DetermineValue(reader, converter, type)); reader.ReadEndElement(); } // use the surrogate to populate the instance initializedObject = serializationSurrogate.SetObjectData(initializedObject, info, Context, SurrogateSelector); } } else if (typeof(ISerializable).IsAssignableFrom(actualType)) { // The item implements ISerializable. Create a SerializationInfo object info = new SerializationInfo(actualType, converter); // Populate the collection PopulateSerializationInfo(reader, converter, actualType, info); // Get the specialized Serialization Constructor ConstructorInfo ctor = actualType.GetConstructor(new Type[] { typeof(SerializationInfo), typeof(StreamingContext) }); // Create the object initializedObject = ctor.Invoke(new object[] { info, Context }); } else { // The item does not implement ISerializable. Use reflection to get public // fields and properties. initializedObject = FormatterServices.GetUninitializedObject(actualType); List <MemberInfo> memberList = new List <MemberInfo> (); List <object> valuesList = new List <object> (); // read the first element. reader.ReadStartElement(); while (reader.IsStartElement()) { // Get public fields and members of this type. MemberInfo[] possibleMembers = actualType.GetMember(reader.Name, MemberTypes.Property | MemberTypes.Field, BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance); if (possibleMembers == null) { throw new SerializationException(string.Format("Serialization stream contained element not found in type.{0}", reader.Name)); } if (possibleMembers.Length > 1) { throw new SerializationException(string.Format("More than one member found for tag {0}", reader.Name)); } if (typeof(IList).IsAssignableFrom(possibleMembers[0].ReflectedType)) { // the type is a list, get the list from the initialized object. IList list = GetMemberValue(initializedObject, possibleMembers[0]) as IList; if (list == null) { throw new SerializationException(string.Format("List in object is null. {0}, member {1}", possibleMembers[0].DeclaringType.FullName, possibleMembers[0].Name)); } // read the next element reader.ReadStartElement(); while (reader.IsStartElement()) { if (!reader.IsEmptyElement) { // Initialize the object (recursive call) object listItem = InitializeObject(reader, converter, possibleMembers[0].ReflectedType.GetElementType()); list.Add(listItem); reader.ReadEndElement(); } else { reader.ReadStartElement(); } } } else { // determine the value. object value = DetermineValue(reader, converter, possibleMembers[0].ReflectedType); memberList.Add(possibleMembers[0]); valuesList.Add(value); } } if (memberList.Count > 0) { initializedObject = FormatterServices.PopulateObjectMembers(initializedObject, memberList.ToArray(), valuesList.ToArray()); } reader.ReadEndElement(); } if ((initializedObject as IDeserializationCallback) != null) { DeserializationCallBackList.Add(initializedObject); } return(initializedObject); }
/// <summary> /// Gets all the serializable members of an object and return an enumarable collection. /// </summary> /// <param name="objectToSerialize">The object to get the members from.</param> /// <param name="objectToSerializeType">The type of the object.</param> /// <param name="converter">The converter to use when converting simple types.</param> /// <returns>An IEnumerable list of <see cref="CustomSerializationEntry"/> entries.</returns> private IEnumerable <CustomSerializationEntry> GetMemberInfo(object objectToSerialize, Type objectToSerializeType, FormatterConverter converter) { ISurrogateSelector selector1 = null; ISerializationSurrogate serializationSurrogate = null; SerializationInfo info = null; Type objectType = objectToSerializeType; // if the passed object is null, break the iteration. if (objectToSerialize == null) { yield break; } if ((SurrogateSelector != null) && ((serializationSurrogate = SurrogateSelector.GetSurrogate(objectType, Context, out selector1)) != null)) { // use a surrogate to get the members. info = new SerializationInfo(objectType, converter); if (!objectType.IsPrimitive) { // get the data from the surrogate. serializationSurrogate.GetObjectData(objectToSerialize, info, Context); } } else if (objectToSerialize is ISerializable) { // object implements ISerializable if (!objectType.IsSerializable) { throw new SerializationException(string.Format("Type is not serializable : {0}.", objectType.FullName)); } info = new SerializationInfo(objectType, converter); // get the data using ISerializable. ((ISerializable)objectToSerialize).GetObjectData(info, Context); } if (info != null) { // either the surrogate provided the members, or the members were retrieved // using ISerializable. // create the custom entries collection by copying all the members foreach (SerializationEntry member in info) { CustomSerializationEntry entry = new CustomSerializationEntry(member.Name, member.ObjectType, false, member.Value); // yield return will return the entry now and return to this point when // the enclosing loop (the one that contains the call to this method) // request the next item. yield return(entry); } } else { // The item does not hava surrogate, not does it implement ISerializable. // We use reflection to get the objects state. if (!objectType.IsSerializable) { throw new SerializationException(string.Format("Type is not serializable : {0}.", objectType.FullName)); } // Get all serializable members MemberInfo[] members = FormatterServices.GetSerializableMembers(objectType, Context); foreach (MemberInfo member in members) { if (CanSerialize(member)) { // create the entry CustomSerializationEntry entry = new CustomSerializationEntry(member.Name, member.ReflectedType, false); if (typeof(IList).IsAssignableFrom(entry.ObjectType)) { entry.IsList = true; } // get the value of the member entry.Value = GetMemberValue(objectToSerialize, member); // yield return will return the entry now and return to this point when // the enclosing loop (the one that contains the call to this method) // request the next item. yield return(entry); } } } }