System.Configuration.GenericEnumConverter.ConvertFrom C# (CSharp) Метод

ConvertFrom() публичный Метод

public ConvertFrom ( ITypeDescriptorContext ctx, CultureInfo ci, object data ) : object
ctx ITypeDescriptorContext
ci System.Globalization.CultureInfo
data object
Результат object
        public override object ConvertFrom(ITypeDescriptorContext ctx, CultureInfo ci, object data) {
            object result = null;
            //
            // For any error, throw the ArgumentException with SR.Invalid_enum_value
            //
            try {
                string value = (string)data;
                if (String.IsNullOrEmpty(value)) {
                    throw new Exception();
                }

                // Disallow numeric values for enums.
                if (!String.IsNullOrEmpty(value) &&
                        (Char.IsDigit(value[0]) ||
                        (value[0] == '-') ||
                        (value[0] == '+'))) {
                    throw new Exception();
                }

                if (value != value.Trim()) { // throw if the value has whitespace 
                    throw new Exception();
                }

                result = Enum.Parse(_enumType, value);
            }
            catch {
                StringBuilder names = new StringBuilder();

                foreach (string name in Enum.GetNames(_enumType)) {
                    if (names.Length != 0) {
                        names.Append(", ");
                    }
                    names.Append(name);
                }
                throw new ArgumentException(SR.GetString(SR.Invalid_enum_value, names.ToString()));
            }
            return result;
        }
    }

Usage Example

Пример #1
0
		public void ConvertFrom_InvalidString ()
		{
			GenericEnumConverter cv = new GenericEnumConverter (typeof (FooEnum));
			object o;

			o = cv.ConvertFrom (null, null, "baz");
			Assert.IsNull (o, "A1");
		}
All Usage Examples Of System.Configuration.GenericEnumConverter::ConvertFrom