[corlib] Update ValueTuple implementation
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / System.Activities.Presentation / System / Activities / Presentation / Base / Core / Internal / PropertyEditing / Editors / FlagStringConverter.cs
1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //----------------------------------------------------------------
4
5 namespace System.Activities.Presentation.Internal.PropertyEditing.Editors
6 {
7     using System.Windows.Data;
8     using System.Globalization;
9     using System.Collections.Generic;
10     using System.Runtime;
11     using System.Collections;
12     sealed class FlagStringConverter : IValueConverter
13     {
14         public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
15         {
16             if (value != null)
17             {
18                 return value.ToString();
19             }
20             else
21             {
22                 return string.Empty;
23             }
24         }
25
26         public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
27         {
28             Type flagType = parameter as Type;
29             Fx.Assert(flagType != null && flagType.IsEnum, "TargetType should be enum");
30             Fx.Assert(Attribute.IsDefined(flagType, typeof(FlagsAttribute)), "FlagType should have flags attribute");
31
32             if (value == null)
33             {
34                 return Enum.ToObject(flagType, 0);
35             }
36
37             string str = (value as string).ToUpperInvariant();
38             str = str.Trim();
39             if (str.Equals(string.Empty) || str.Equals("0"))
40             {
41                 return Enum.ToObject(flagType, 0);
42             }
43
44             Dictionary<string, object> flagDictionary = GenerateFlagDictionary(flagType);
45             int flagsIntValue = 0;
46             string[] names = str.Split(',');
47             foreach (string name in names)
48             {
49                 string flagName = name.Trim();
50                 if (flagDictionary.ContainsKey(flagName))
51                 {
52                     flagsIntValue |= (int)flagDictionary[flagName];
53                     flagDictionary.Remove(flagName);
54                 }                
55                 else
56                 {
57                     throw FxTrace.Exception.AsError(new ArgumentException(string.Format(CultureInfo.CurrentUICulture, SR.InvalidFlagName, value, flagType.Name)));
58                 }
59             }
60             return Enum.ToObject(flagType, flagsIntValue);
61         }
62
63         static Dictionary<string, object> GenerateFlagDictionary(Type flagType)
64         {
65             Dictionary<string, object> flagDictionary = new Dictionary<string, object>();
66             string[] flagNames = flagType.GetEnumNames();
67             Array flagValues = flagType.GetEnumValues();
68             for (int i = 0; i < flagNames.Length; i++)
69             {
70                 flagDictionary.Add(flagNames[i].ToUpperInvariant(), flagValues.GetValue(i));
71             }
72             return flagDictionary;
73         }
74     }
75 }