SerializedFromResXHandler.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //
  2. // SerializedFromResXHandler.cs : Handles a resource that was stored in a
  3. // resx file by means of serialization.
  4. //
  5. // Author:
  6. // Gary Barnett ([email protected])
  7. //
  8. // Copyright (C) Gary Barnett (2012)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. using System;
  29. using System.Reflection;
  30. using System.ComponentModel.Design;
  31. using System.ComponentModel;
  32. using System.Runtime.Serialization;
  33. using System.Runtime.Serialization.Formatters.Binary;
  34. using System.IO;
  35. using System.Text;
  36. using System.Runtime.Serialization.Formatters.Soap;
  37. namespace System.Resources {
  38. internal class SerializedFromResXHandler : ResXDataNodeHandler, IWritableHandler {
  39. string dataString;
  40. string mime_type;
  41. CustomBinder binder; // so type set after first call
  42. public SerializedFromResXHandler (string data, string _mime_type)
  43. {
  44. dataString = data;
  45. mime_type = _mime_type;
  46. }
  47. #region implemented abstract members of System.Resources.ResXDataNodeHandler
  48. public override object GetValue (ITypeResolutionService typeResolver)
  49. {
  50. return DeserializeObject (typeResolver);
  51. }
  52. public override object GetValue (AssemblyName [] assemblyNames)
  53. {
  54. return DeserializeObject (new AssemblyNamesTypeResolutionService (assemblyNames));
  55. }
  56. public override string GetValueTypeName (ITypeResolutionService typeResolver)
  57. {
  58. return InternalGetValueType (typeResolver);
  59. }
  60. public override string GetValueTypeName (AssemblyName [] assemblyNames)
  61. {
  62. return InternalGetValueType (null);
  63. }
  64. #endregion
  65. #region IWritableHandler implementation
  66. public string DataString {
  67. get {
  68. return dataString;
  69. }
  70. }
  71. #endregion
  72. string InternalGetValueType (ITypeResolutionService typeResolver)
  73. {
  74. object retrievedObject;
  75. try {
  76. retrievedObject = DeserializeObject (typeResolver);
  77. } catch {
  78. return typeof (object).AssemblyQualifiedName;
  79. }
  80. if (retrievedObject == null)
  81. return null;
  82. else
  83. return retrievedObject.GetType ().AssemblyQualifiedName;
  84. }
  85. object DeserializeObject (ITypeResolutionService typeResolver)
  86. {
  87. try {
  88. if (mime_type == ResXResourceWriter.SoapSerializedObjectMimeType) {
  89. //FIXME: theres a test in the suite to check that a type converter converts from invariant string
  90. //do i need to take the string culture into consideration here?
  91. SoapFormatter soapF = new SoapFormatter ();
  92. if (binder == null)
  93. binder = new CustomBinder (typeResolver);
  94. soapF.Binder = binder;
  95. byte [] data = Convert.FromBase64String (dataString);
  96. using (MemoryStream s = new MemoryStream (data)) {
  97. return soapF.Deserialize (s);
  98. }
  99. } else if (mime_type == ResXResourceWriter.BinSerializedObjectMimeType) {
  100. BinaryFormatter binF = new BinaryFormatter ();
  101. if (binder == null)
  102. binder = new CustomBinder (typeResolver);
  103. binF.Binder = binder;
  104. byte [] data = Convert.FromBase64String (dataString);
  105. using (MemoryStream s = new MemoryStream (data)) {
  106. return binF.Deserialize (s);
  107. }
  108. } else // invalid mime_type
  109. return null;
  110. } catch (SerializationException ex) {
  111. if (ex.Message.StartsWith ("Couldn't find assembly"))
  112. throw new ArgumentException (ex.Message);
  113. else
  114. throw ex;
  115. }
  116. }
  117. sealed class CustomBinder : SerializationBinder
  118. {
  119. ITypeResolutionService typeResolver;
  120. public CustomBinder (ITypeResolutionService _typeResolver)
  121. {
  122. // nulls ok
  123. typeResolver = _typeResolver;
  124. }
  125. public override Type BindToType(string assemblyName, string typeName)
  126. {
  127. Type typeToUse = null;
  128. string typeString = String.Format("{0}, {1}", typeName, assemblyName);
  129. if (typeResolver != null)
  130. typeToUse = typeResolver.GetType (typeString);
  131. if (typeToUse == null)
  132. typeToUse = Type.GetType(typeString);
  133. return typeToUse;
  134. }
  135. }
  136. }
  137. }