Microsoft.Tools.WindowsInstaller.ReinstallModesConverter.ConvertFrom C# (CSharp) Method

ConvertFrom() public method

Converts a String in the short form like "omus" to a ReinstallModes enumeration.
The short form string contains invalid characters.
public ConvertFrom ( ITypeDescriptorContext context, CultureInfo culture, object value ) : object
context ITypeDescriptorContext Additional context for conversion.
culture System.Globalization.CultureInfo The culture to use for conversion.
value object The value to convert.
return object
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (null != value && this.CanConvertFrom(context, value.GetType()))
            {
                string s = value as string;
                ReinstallModes mode = 0;

                // Attempt the simple coversion.
                if (TryParse(s, out mode))
                {
                    return mode;
                }
                else
                {
                    // Try parsing the REINSTALLMODE property values.
                    foreach (char c in s)
                    {
                        if (CharToModeMap.ContainsKey(c))
                        {
                            mode |= CharToModeMap[c];
                        }
                        else
                        {
                            throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.Error_InvalidReinstallMode, c), "value");
                        }
                    }

                    return mode;
                }
            }

            return base.ConvertFrom(context, culture, value);
        }

Usage Example

        public void ConvertShortFormToReinstallModes()
        {
            var converter = new ReinstallModesConverter();
            Assert.IsTrue(converter.CanConvertFrom(typeof(string)));

            var mode = (ReinstallModes)converter.ConvertFrom("omUS");
            Assert.AreEqual(Default, mode);
        }
All Usage Examples Of Microsoft.Tools.WindowsInstaller.ReinstallModesConverter::ConvertFrom