dff1f8fd22d0294aa87675747ba37e0252ca61b0
[mono.git] / mcs / class / referencesource / System.ServiceModel / System / ServiceModel / Configuration / ReliableMessagingVersionConverter.cs
1 //-----------------------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //-----------------------------------------------------------------------------
4
5 namespace System.ServiceModel.Configuration
6 {
7     //using System;
8     using System.ComponentModel;
9     using System.ComponentModel.Design.Serialization;
10     using System.Globalization;
11
12     class ReliableMessagingVersionConverter : TypeConverter
13     {
14         public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
15         {
16             if (typeof(string) == sourceType)
17             {
18                 return true;
19             }
20             return base.CanConvertFrom(context, sourceType);
21         }
22
23         public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
24         {
25             if (typeof(InstanceDescriptor) == destinationType)
26             {
27                 return true;
28             }
29             return base.CanConvertTo(context, destinationType);
30         }
31
32         public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
33         {
34             string version = value as string;
35 #pragma warning suppress 56507 // Microsoft, Really checking for null (meaning value was not a string) versus String.Empty
36             if (version != null)
37             {
38                 switch (version)
39                 {
40                     case ConfigurationStrings.Default:
41                         return ReliableMessagingVersion.Default;
42                     case ConfigurationStrings.WSReliableMessaging11:
43                         return ReliableMessagingVersion.WSReliableMessaging11;
44                     case ConfigurationStrings.WSReliableMessagingFebruary2005:
45                         return ReliableMessagingVersion.WSReliableMessagingFebruary2005;
46                     default:
47                         throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.GetString(SR.ConfigInvalidReliableMessagingVersionValue, version));
48                 }
49             }
50             return base.ConvertFrom(context, culture, value);
51         }
52
53         public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
54         {
55             if (typeof(string) == destinationType && value is ReliableMessagingVersion)
56             {
57                 ReliableMessagingVersion version = (ReliableMessagingVersion)value;
58                 
59                 if (version == ReliableMessagingVersion.Default)
60                 {
61                     return ConfigurationStrings.Default;
62                 }
63                 else if (version == ReliableMessagingVersion.WSReliableMessaging11)
64                 {
65                     return ConfigurationStrings.WSReliableMessaging11;
66                 }
67                 else if (version == ReliableMessagingVersion.WSReliableMessagingFebruary2005)
68                 {
69                     return ConfigurationStrings.WSReliableMessagingFebruary2005;
70                 }
71                 else
72                 {
73                     throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value",
74                         SR.GetString(SR.ConfigInvalidClassInstanceValue, typeof(ReliableMessagingVersion).FullName)));
75                 }
76             }
77             return base.ConvertTo(context, culture, value, destinationType);
78         }
79     }
80 }