2 // TypeConverterFromResXHandler.cs : Handles a resource that was stored
3 // in a resx file by means of a typeconverter associated with the
7 // Gary Barnett (gary.barnett.mono@gmail.com)
9 // Copyright (C) Gary Barnett (2012)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
31 using System.Reflection;
32 using System.ComponentModel.Design;
33 using System.ComponentModel;
35 namespace System.Resources {
36 internal class TypeConverterFromResXHandler : ResXDataNodeHandler, IWritableHandler {
42 public TypeConverterFromResXHandler (string data, string _mime_type, string _typeString)
45 mime_type = _mime_type;
46 typeString = _typeString;
49 #region implemented abstract members of System.Resources.ResXDataNodeHandler
50 public override object GetValue (ITypeResolutionService typeResolver)
52 if (!String.IsNullOrEmpty(mime_type)
53 && mime_type != ResXResourceWriter.ByteArraySerializedObjectMimeType)
56 Type type = ResolveType (typeString, typeResolver);
58 throw new TypeLoadException();
60 TypeConverter c = TypeDescriptor.GetConverter (type);
62 throw new TypeLoadException();
64 return ConvertData (c);
67 public override object GetValue (AssemblyName[] assemblyNames)
69 if (!String.IsNullOrEmpty(mime_type)
70 && mime_type != ResXResourceWriter.ByteArraySerializedObjectMimeType)
73 Type type = ResolveType (typeString, assemblyNames);
75 throw new TypeLoadException();
77 TypeConverter c = TypeDescriptor.GetConverter (type);
79 throw new TypeLoadException();
81 return ConvertData (c);
84 public override string GetValueTypeName (ITypeResolutionService typeResolver)
86 Type type = ResolveType (typeString, typeResolver);
91 return type.AssemblyQualifiedName;
94 public override string GetValueTypeName (AssemblyName [] assemblyNames)
96 Type type = ResolveType (typeString, assemblyNames);
101 return type.AssemblyQualifiedName;
105 #region IWritableHandler implementation
106 public string DataString {
113 object ConvertData (TypeConverter c)
115 if (mime_type == ResXResourceWriter.ByteArraySerializedObjectMimeType) {
116 if (c.CanConvertFrom (typeof (byte [])))
117 return c.ConvertFrom (Convert.FromBase64String (dataString));
118 } else if (String.IsNullOrEmpty (mime_type)) {
119 if (c.CanConvertFrom (typeof (string)))
120 return c.ConvertFromInvariantString (dataString);
123 throw new Exception ("shouldnt get here, invalid mime type");
125 throw new TypeLoadException ("No converter for this type found");