public static void Save(object obj, string path) { BinaryFormatter bf = new BinaryFormatter(); FileStream file = File.Create(path); if (ss == null) { CreateSurrogates(); } if (Texture2DCompressionType_old != Texture2DCompressionType) { ss.RemoveSurrogate(typeof(Texture2D), new StreamingContext(StreamingContextStates.All)); Texture2DSerializationSurrogate texture_ss = new Texture2DSerializationSurrogate(); ss.AddSurrogate(typeof(Texture2D), new StreamingContext(StreamingContextStates.All), texture_ss); Texture2DCompressionType_old = Texture2DCompressionType; } bf.SurrogateSelector = ss; bf.Serialize(file, obj); file.Close(); }
public void RemoveSurrogate_InvalidArguments_ThrowExceptions() { var s = new SurrogateSelector(); var c = new StreamingContext(); AssertExtensions.Throws <ArgumentNullException>("type", () => s.RemoveSurrogate(null, c)); s.RemoveSurrogate(typeof(string), c); // no exception even if removal fails }
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 Close() { try { if (inited) { foreach (ISurrogate surrogate in Extensions.GetSurrogates()) { ss.RemoveSurrogate(surrogate.type, new StreamingContext(StreamingContextStates.All)); } dataPath = String.Empty; inited = false; } return(true); } catch (System.Exception e) { Debug.LogError("Cleared failed : " + e); 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; strLoc = "Loc_100aa"; surrSelect1 = new SurrogateSelector(); iCountTestcases++; try { surrSelect1.RemoveSurrogate(null, context); iCountErrors++; printerr("Error_100bb!! Expected ArgumentException not thrown"); } catch (ArgumentException) {} catch (Exception exc) { iCountErrors++; printerr("Error_100dd!! Incorrect Exception thrown : " + exc.ToString()); } strLoc = "Loc_200aa"; surrSelect1 = new SurrogateSelector(); iCountTestcases++; surrSelect1.RemoveSurrogate(typeof(String), context); 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"); } surrSelect1.RemoveSurrogate(typeof(Int64), context); iCountTestcases++; if (surrSelect1.GetSurrogate(typeof(Int64), context, out ss) != null) { iCountErrors++; printerr("Error_350cd! ISerializationSurrgate not removed"); } iCountTestcases++; if (!surrSelect1.GetSurrogate(typeof(Int32), context, out ss).Equals(testss3)) { iCountErrors++; printerr("Error_350dd! Incorrect IserializationSurrogate returned"); } surrSelect1.RemoveSurrogate(typeof(Int32), context); iCountTestcases++; if (surrSelect1.GetSurrogate(typeof(Int32), context, out ss) != null) { iCountErrors++; printerr("Error_350de! SerializationSurrogate not removed properly"); } 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); surrSelect1.AddSurrogate(typeof(Guid), context, testss1); surrSelect2.AddSurrogate(typeof(Decimal), context, testss2); surrSelect3.AddSurrogate(typeof(Int32), context, testss3); surrSelect4.AddSurrogate(typeof(Exception), context, testss4); surrSelect5.AddSurrogate(typeof(Single), context, testss5); surrSelect3.RemoveSurrogate(typeof(Int64), context); if (surrSelect1.GetSurrogate(typeof(Int32), context, out ss) == null) { iCountErrors++; printerr("Error_400bb!! Did not find Surrogate in chain"); } strLoc = "Loc_401aa"; surrSelect1.RemoveSurrogate(typeof(Single), context); surrSelect1.RemoveSurrogate(typeof(Decimal), context); iCountTestcases++; if (surrSelect1.GetSurrogate(typeof(Single), context, out ss) == null) { iCountErrors++; printerr("Error_401bb! Incorrectly removed surrogates further down the chain"); } iCountTestcases++; if (surrSelect1.GetSurrogate(typeof(Decimal), context, out ss) == null) { iCountErrors++; printerr("Error_401cc! Incorrectly removed suggogate further down the chain"); } strLoc = "Loc_403aa"; surrSelect3.RemoveSurrogate(typeof(Int32), context); iCountTestcases++; if (surrSelect1.GetSurrogate(typeof(Int32), context, out ss) != null) { iCountErrors++; printerr("Error_403bb! Found a removed surrogate"); } strLoc = "Loc_405aa"; if (surrSelect1.GetSurrogate(typeof(Single), context, out ss) == null) { iCountErrors++; printerr("Error_405bb!! Surrogate in chain not found"); } surrSelect5.RemoveSurrogate(typeof(Single), context); iCountTestcases++; if (surrSelect1.GetSurrogate(typeof(Single), context, out ss) != null) { iCountErrors++; printerr("Error_405cc!! Removed surrogate found???"); } } 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; strLoc = "Loc_100aa"; surrSelect1 = new SurrogateSelector(); iCountTestcases++; try { surrSelect1.RemoveSurrogate(null, context); iCountErrors++; printerr("Error_100bb!! Expected ArgumentException not thrown"); } catch (ArgumentException){} catch (Exception exc) { iCountErrors++; printerr("Error_100dd!! Incorrect Exception thrown : "+exc.ToString()); } strLoc = "Loc_200aa"; surrSelect1 = new SurrogateSelector(); iCountTestcases++; surrSelect1.RemoveSurrogate(typeof(String), context); 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"); } surrSelect1.RemoveSurrogate(typeof(Int64), context); iCountTestcases++; if(surrSelect1.GetSurrogate(typeof(Int64), context, out ss) != null) { iCountErrors++; printerr("Error_350cd! ISerializationSurrgate not removed"); } iCountTestcases++; if(!surrSelect1.GetSurrogate(typeof(Int32), context, out ss).Equals(testss3)) { iCountErrors++; printerr("Error_350dd! Incorrect IserializationSurrogate returned"); } surrSelect1.RemoveSurrogate(typeof(Int32), context); iCountTestcases++; if(surrSelect1.GetSurrogate(typeof(Int32), context, out ss) != null) { iCountErrors++; printerr("Error_350de! SerializationSurrogate not removed properly"); } 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); surrSelect1.AddSurrogate(typeof(Guid), context, testss1); surrSelect2.AddSurrogate(typeof(Decimal), context, testss2); surrSelect3.AddSurrogate(typeof(Int32), context, testss3); surrSelect4.AddSurrogate(typeof(Exception), context, testss4); surrSelect5.AddSurrogate(typeof(Single), context, testss5); surrSelect3.RemoveSurrogate(typeof(Int64), context); if(surrSelect1.GetSurrogate(typeof(Int32), context, out ss) == null) { iCountErrors++; printerr("Error_400bb!! Did not find Surrogate in chain"); } strLoc = "Loc_401aa"; surrSelect1.RemoveSurrogate(typeof(Single), context); surrSelect1.RemoveSurrogate(typeof(Decimal), context); iCountTestcases++; if(surrSelect1.GetSurrogate(typeof(Single), context, out ss) == null) { iCountErrors++; printerr("Error_401bb! Incorrectly removed surrogates further down the chain"); } iCountTestcases++; if(surrSelect1.GetSurrogate(typeof(Decimal), context, out ss) == null) { iCountErrors++; printerr("Error_401cc! Incorrectly removed suggogate further down the chain"); } strLoc = "Loc_403aa"; surrSelect3.RemoveSurrogate(typeof(Int32), context); iCountTestcases++; if(surrSelect1.GetSurrogate(typeof(Int32), context, out ss) != null) { iCountErrors++; printerr("Error_403bb! Found a removed surrogate"); } strLoc = "Loc_405aa"; if(surrSelect1.GetSurrogate(typeof(Single), context, out ss) == null) { iCountErrors++; printerr("Error_405bb!! Surrogate in chain not found"); } surrSelect5.RemoveSurrogate(typeof(Single), context); iCountTestcases++; if(surrSelect1.GetSurrogate(typeof(Single), context, out ss) != null) { iCountErrors++; printerr("Error_405cc!! Removed surrogate found???"); } } 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; } }