ResXFileRef.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Permission is hereby granted, free of charge, to any person obtaining
  2. // a copy of this software and associated documentation files (the
  3. // "Software"), to deal in the Software without restriction, including
  4. // without limitation the rights to use, copy, modify, merge, publish,
  5. // distribute, sublicense, and/or sell copies of the Software, and to
  6. // permit persons to whom the Software is furnished to do so, subject to
  7. // the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  16. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  17. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  18. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. //
  20. // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
  21. //
  22. // Authors:
  23. // Peter Bartok ([email protected])
  24. //
  25. //
  26. // COMPLETE
  27. using System;
  28. using System.ComponentModel;
  29. using System.IO;
  30. using System.Reflection;
  31. namespace System.Resources {
  32. [Serializable]
  33. [TypeConverter(typeof(ResXFileRef.Converter))]
  34. public class ResXFileRef {
  35. #region Converter Class
  36. public class Converter : TypeConverter {
  37. #region Constructors
  38. public Converter() {
  39. }
  40. #endregion // Constructors
  41. #region Methods
  42. public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) {
  43. return sourceType == typeof(string);
  44. }
  45. public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) {
  46. return destinationType == typeof(string);
  47. }
  48. public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) {
  49. string[] parts;
  50. byte[] buffer;
  51. if ( !(value is String)) {
  52. return base.ConvertFrom(context, culture, value);
  53. }
  54. parts = ((string)value).Split(';');
  55. using (FileStream file = new FileStream(parts[0], FileMode.Open, FileAccess.Read, FileShare.Read)) {
  56. buffer = new byte[file.Length];
  57. file.Read(buffer, 0, (int)file.Length);
  58. }
  59. return Activator.CreateInstance(Type.GetType(parts[1]), BindingFlags.CreateInstance | BindingFlags.Public | BindingFlags.Instance, null, new object[] { new MemoryStream(buffer) }, culture);
  60. }
  61. public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) {
  62. if (destinationType != typeof(String)) {
  63. return base.ConvertTo (context, culture, value, destinationType);
  64. }
  65. return ((ResXFileRef)value).ToString();
  66. }
  67. #endregion // Methods
  68. }
  69. #endregion // Converter Class
  70. #region Local Variables
  71. private string filename;
  72. private string typename;
  73. #endregion // Local Variables
  74. #region Public Constructors
  75. public ResXFileRef(string fileName, string typeName) {
  76. this.filename = fileName;
  77. this.typename = typeName;
  78. }
  79. #endregion // Public Constructors
  80. #region Public Instance Properties
  81. #endregion // Public Instance Properties
  82. #region Public Instance Methods
  83. public override string ToString() {
  84. return filename + ";" + typename;
  85. }
  86. #endregion // Public Instance Methods
  87. }
  88. }