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