VersionConverter.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. //
  2. // Authors:
  3. // Marek Habersack <[email protected]>
  4. //
  5. // Copyright (C) 2010 Novell, Inc (http://novell.com/)
  6. //
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining
  9. // a copy of this software and associated documentation files (the
  10. // "Software"), to deal in the Software without restriction, including
  11. // without limitation the rights to use, copy, modify, merge, publish,
  12. // distribute, sublicense, and/or sell copies of the Software, and to
  13. // permit persons to whom the Software is furnished to do so, subject to
  14. // the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be
  17. // included in all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  23. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  24. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  25. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. //
  27. using System;
  28. using System.Configuration;
  29. using System.ComponentModel;
  30. using System.Globalization;
  31. namespace System.Web.Configuration
  32. {
  33. sealed class VersionConverter : ConfigurationConverterBase
  34. {
  35. Version minVersion;
  36. string exceptionText;
  37. public VersionConverter ()
  38. {
  39. }
  40. public VersionConverter (int minMajor, int minMinor, string exceptionText = null)
  41. {
  42. minVersion = new Version (minMajor, minMinor);
  43. this.exceptionText = exceptionText;
  44. }
  45. public override object ConvertFrom (ITypeDescriptorContext ctx, CultureInfo ci, object data)
  46. {
  47. string input = data as string;
  48. if (String.IsNullOrEmpty (input))
  49. throw new ConfigurationErrorsException ("The input string is too short or null.");
  50. Version result;
  51. if (!Version.TryParse (input, out result))
  52. throw new ConfigurationErrorsException ("The input string wasn't in correct format.");
  53. if (minVersion != null && result < minVersion)
  54. throw new ConfigurationErrorsException (String.Format (exceptionText, result, minVersion));
  55. return result;
  56. }
  57. public override object ConvertTo (ITypeDescriptorContext ctx, CultureInfo ci, object value, Type type)
  58. {
  59. Version ver = value as Version;
  60. if (ver == null)
  61. throw new ArgumentException ("Is not an instance of the Version type", "value");
  62. if (type == typeof (string))
  63. return ver.ToString ();
  64. if (type == typeof (Version))
  65. return ver.Clone ();
  66. throw new ConfigurationErrorsException ("Conversion to type '" + type + "' is not supported.");
  67. }
  68. }
  69. }