HelperClasses_Resources.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. //
  2. // HelperClasses_Resources.cs : Various types to use as resources during
  3. // testing. Note some more are present in DummyAssembly project and
  4. // WriterTest.cs
  5. //
  6. // Author:
  7. // Gary Barnett ([email protected])
  8. //
  9. // Copyright (C) Gary Barnett (2012)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. using System;
  30. using System.Runtime.Serialization;
  31. using System.Runtime.Serialization.Formatters.Binary;
  32. using System.ComponentModel;
  33. using System.Globalization;
  34. using System.IO;
  35. namespace MonoTests.System.Resources {
  36. class notserializable {
  37. public object test;
  38. public notserializable ()
  39. {
  40. }
  41. }
  42. [SerializableAttribute]
  43. public class serializable : ISerializable {
  44. public string name;
  45. public string value;
  46. public serializable ()
  47. {
  48. }
  49. public serializable (string name, string value)
  50. {
  51. this.name = name;
  52. this.value = value;
  53. }
  54. public serializable (SerializationInfo info, StreamingContext ctxt)
  55. {
  56. name = (string) info.GetValue ("sername", typeof (string));
  57. value = (String) info.GetValue ("servalue", typeof (string));
  58. }
  59. public serializable (Stream stream)
  60. {
  61. BinaryFormatter bFormatter = new BinaryFormatter ();
  62. serializable deser = (serializable) bFormatter.Deserialize (stream);
  63. stream.Close ();
  64. name = deser.name;
  65. value = deser.value;
  66. }
  67. public void GetObjectData (SerializationInfo info, StreamingContext ctxt)
  68. {
  69. info.AddValue ("sername", name);
  70. info.AddValue ("servalue", value);
  71. }
  72. public override string ToString ()
  73. {
  74. return String.Format ("name={0};value={1}", this.name, this.value);
  75. }
  76. public override bool Equals (object obj)
  77. {
  78. serializable o = obj as serializable;
  79. if (o == null)
  80. return false;
  81. return this.name.Equals (o.name) && this.value.Equals (o.value);
  82. }
  83. }
  84. [SerializableAttribute]
  85. public class serializableSubClass : serializable {
  86. public serializableSubClass ()
  87. {
  88. }
  89. public serializableSubClass (SerializationInfo info, StreamingContext ctxt)
  90. : base (info, ctxt)
  91. {
  92. }
  93. public serializableSubClass (Stream stream)
  94. {
  95. BinaryFormatter bFormatter = new BinaryFormatter ();
  96. serializableSubClass deser = (serializableSubClass) bFormatter.Deserialize (stream);
  97. stream.Close ();
  98. name = deser.name;
  99. value = deser.value;
  100. }
  101. }
  102. [SerializableAttribute]
  103. [TypeConverter (typeof (ThisAssemblyConvertableConverter))]
  104. public class ThisAssemblyConvertable {
  105. protected string name;
  106. protected string value;
  107. public ThisAssemblyConvertable ()
  108. {
  109. }
  110. public ThisAssemblyConvertable (string name, string value)
  111. {
  112. this.name = name;
  113. this.value = value;
  114. }
  115. public void GetObjectData (SerializationInfo info, StreamingContext ctxt)
  116. {
  117. info.AddValue ("sername", name);
  118. info.AddValue ("servalue", value);
  119. }
  120. public override string ToString ()
  121. {
  122. return String.Format ("{0}\t{1}", name, value);
  123. }
  124. public override bool Equals (object obj)
  125. {
  126. ThisAssemblyConvertable o = obj as ThisAssemblyConvertable;
  127. if (o == null)
  128. return false;
  129. return this.name.Equals (o.name) && this.value.Equals (o.value);
  130. }
  131. }
  132. class ThisAssemblyConvertableConverter : TypeConverter {
  133. public ThisAssemblyConvertableConverter ()
  134. {
  135. }
  136. public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
  137. {
  138. return sourceType == typeof (string);
  139. }
  140. public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
  141. {
  142. return destinationType == typeof (string);
  143. }
  144. public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
  145. {
  146. if (value.GetType () != typeof (string))
  147. throw new Exception ("value not string");
  148. string serialised = (string) value;
  149. string [] parts = serialised.Split ('\t');
  150. if (parts.Length != 2)
  151. throw new Exception ("string in incorrect format");
  152. ThisAssemblyConvertable convertable = new ThisAssemblyConvertable (parts [0], parts [1]);
  153. return convertable;
  154. }
  155. public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) {
  156. if (destinationType != typeof (String)) {
  157. return base.ConvertTo (context, culture, value, destinationType);
  158. }
  159. return ((ThisAssemblyConvertable) value).ToString ();
  160. }
  161. }
  162. }