Microsoft.Tools.WindowsInstaller.LoggingPoliciesConverter.ConvertFrom C# (CSharp) Méthode

ConvertFrom() public méthode

Converts a String in the short form like "omus" to a LoggingPolicies 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.
Résultat object
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (null != value && this.CanConvertFrom(context, value.GetType()))
            {
                var s = value as string;
                var mode = LoggingPolicies.None;

                // Attempt the simple coversion.
                if (TryParse(s, out mode))
                {
                    return mode;
                }
                else
                {
                    // Try parsing the logging command line options.
                    foreach (var c in s)
                    {
                        if (CharToModeMap.ContainsKey(c))
                        {
                            mode |= CharToModeMap[c];
                        }
                        else if ('+' == c)
                        {
                            throw new ArgumentException(Resources.Error_UnsupportedLoggingMode, "value");
                        }
                        else
                        {
                            var message = String.Format(CultureInfo.CurrentCulture, Resources.Error_InvalidLoggingMode, c);
                            throw new ArgumentException(message, "value");
                        }
                    }

                    return mode;
                }
            }

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

Usage Example

        public void ConvertInvalidStringToLoggingModes()
        {
            var converter = new LoggingPoliciesConverter();
            Assert.IsTrue(converter.CanConvertFrom(typeof(string)));
            Assert.IsFalse(converter.CanConvertFrom(this.GetType()));

            // The append logging command line option is not supported in the registry policy.
            ExceptionAssert.Throws<ArgumentException>(() => { var mode = (LoggingPolicies)converter.ConvertFrom("*vx!+"); });

            // Bogus command line options.
            ExceptionAssert.Throws<ArgumentException>(() => { var mode = (LoggingPolicies)converter.ConvertFrom("jkl"); });
        }
All Usage Examples Of Microsoft.Tools.WindowsInstaller.LoggingPoliciesConverter::ConvertFrom