FileRefHandler.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //
  2. // FileRefHandler.cs : Handles a ResXFileRef that was either stored in
  3. // a resx file or has been added to a ResXDataNode that was freshly
  4. // instantiated by a user
  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.Reflection;
  31. using System.ComponentModel.Design;
  32. using System.ComponentModel;
  33. using System.IO;
  34. namespace System.Resources {
  35. internal class FileRefHandler : ResXDataNodeHandler {
  36. ResXFileRef resXFileRef; // same as that referenced in ResXDataNode
  37. public FileRefHandler (ResXFileRef fileRef)
  38. {
  39. resXFileRef = fileRef;
  40. }
  41. #region implemented abstract members of System.Resources.ResXDataNodeHandler
  42. public override object GetValue (ITypeResolutionService typeResolver)
  43. {
  44. return GetValue ();
  45. }
  46. public override object GetValue (AssemblyName [] assemblyNames)
  47. {
  48. return GetValue ();
  49. }
  50. public override string GetValueTypeName (ITypeResolutionService typeResolver)
  51. {
  52. // although params ignored by GetValue. .NET resolves the type for GetValueTypeName
  53. Type type = ResolveType (resXFileRef.TypeName, typeResolver);
  54. if (type == null)
  55. return resXFileRef.TypeName;
  56. else
  57. return type.AssemblyQualifiedName;
  58. }
  59. public override string GetValueTypeName (AssemblyName [] assemblyNames)
  60. {
  61. Type type = ResolveType (resXFileRef.TypeName, assemblyNames);
  62. if (type == null)
  63. return resXFileRef.TypeName;
  64. else
  65. return type.AssemblyQualifiedName;
  66. }
  67. #endregion
  68. private object GetValue ()
  69. {
  70. TypeConverter c = TypeDescriptor.GetConverter (typeof (ResXFileRef));
  71. try {
  72. return c.ConvertFromInvariantString (resXFileRef.ToString ());
  73. } catch (ArgumentNullException ex) {
  74. if (ex.ParamName == "type")
  75. throw new TypeLoadException ("Could not find type", ex); //FIXME: error message?
  76. else
  77. throw ex;
  78. }
  79. }
  80. }
  81. }