Пример #1
0
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if ((value as string) != null)
            {
                Type _type = Nullable.GetUnderlyingType(this.type);
                try
                {
                    string strValue = ((string)value);
                    if (strValue.Length == 0)
                    {
                        return(null);
                    }

                    if (StiPropertyGridOptions.Localizable)
                    {
                        string[] strs = Enum.GetNames(_type);
                        foreach (string str in strs)
                        {
                            string locName = StiLocalization.Get("PropertyEnum", _type.Name + str, false);
                            if (locName != null && locName == strValue)
                            {
                                strValue = str;
                            }
                        }
                    }

                    if (strValue.IndexOf(',') != -1)
                    {
                        long      num     = 0;
                        string [] strings = strValue.Split(new char[] { ',' });
                        for (int index = 0; (index < strings.Length); index++)
                        {
                            num = (num | Convert.ToInt64(((Enum)Enum.Parse(_type, strings[index], true))));
                        }
                        return(Enum.ToObject(_type, num));
                    }

                    return(Enum.Parse(_type, strValue, true));
                }
                catch
                {
                    throw new Exception("ConvertInvalidPrimitive");
                }
            }

            return(base.ConvertFrom(context, culture, value));
        }
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(string))
            {
                bool boolValue = false;
                if (value is bool)
                {
                    boolValue = (bool)value;
                }

                if (boolValue)
                {
                    string strTrue = null;

                    if (StiPropertyGridOptions.Localizable)
                    {
                        strTrue = StiLocalization.Get("PropertyEnum", "boolTrue");
                    }
                    if (strTrue == null)
                    {
                        strTrue = "True";
                    }
                    return(strTrue);
                }
                else
                {
                    string strFalse = null;
                    if (StiPropertyGridOptions.Localizable)
                    {
                        strFalse = StiLocalization.Get("PropertyEnum", "boolFalse");
                    }
                    if (strFalse == null)
                    {
                        strFalse = "False";
                    }
                    return(strFalse);
                }
            }
            return(base.ConvertTo(context, destinationType));
        }
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if ((value as string) != null)
            {
                string strValue = ((string)value).Trim();
                try
                {
                    string strTrue = null;
                    if (StiPropertyGridOptions.Localizable)
                    {
                        strTrue = StiLocalization.Get("PropertyEnum", "boolTrue");
                    }
                    if (strTrue == null)
                    {
                        strTrue = "True";
                    }

                    string strFalse = null;
                    if (StiPropertyGridOptions.Localizable)
                    {
                        strFalse = StiLocalization.Get("PropertyEnum", "boolFalse");
                    }
                    if (strFalse == null)
                    {
                        strFalse = "False";
                    }

                    return(strValue == strTrue);
                }
                catch
                {
                    throw new Exception("ConvertInvalidPrimitive");
                }
            }
            return(base.ConvertFrom(context, culture, value));
        }
Пример #4
0
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture,
                                         object value, Type destinationType)
        {
            if (destinationType == null)
            {
                throw new ArgumentNullException("destinationType");
            }
            if (value == null)
            {
                return("null");
            }

            if ((destinationType == typeof(string)) && (value != null))
            {
                if (value is string && (string)value == "null")
                {
                    return(null);
                }
                Type _type = Nullable.GetUnderlyingType(this.type);

                Type underlyingType = Enum.GetUnderlyingType(_type);
                if (((value as IConvertible) != null) && (value.GetType() != underlyingType))
                {
                    value = ((IConvertible)value).ToType(underlyingType, culture);
                }
                if (!this.type.IsDefined(typeof(FlagsAttribute), false) && !Enum.IsDefined(_type, value))
                {
                    throw new Exception("EnumConverterInvalidValue");
                }

                string name = Enum.Format(_type, value, "G");
                if (StiPropertyGridOptions.Localizable)
                {
                    if (name.IndexOf(',') == -1)
                    {
                        string locName = StiLocalization.Get("PropertyEnum", _type.Name + name, false);
                        if (locName == null)
                        {
                            return(name);
                        }
                        return(locName);
                    }
                    else
                    {
                        string [] names = name.Split(new char[] { ',' });
                        string    itog  = string.Empty;

                        foreach (string nm in names)
                        {
                            string str = string.Empty;
                            if (itog != string.Empty)
                            {
                                str += ", ";
                            }
                            string locName = StiLocalization.Get("PropertyEnum", _type.Name + nm.Trim(), false);
                            if (locName != null)
                            {
                                itog += str + locName;
                            }
                            else
                            {
                                itog += str + nm.Trim();
                            }
                        }
                        return(itog);
                    }
                }
                return(name);
            }

            if ((destinationType == typeof(InstanceDescriptor)) && (value != null))
            {
                Type _type = Nullable.GetUnderlyingType(this.type);

                string text1 = base.ConvertToInvariantString(context, value);
                if (_type.IsDefined(typeof(FlagsAttribute), false) && (text1.IndexOf(',') != -1))
                {
                    Type underlyingType = Enum.GetUnderlyingType(_type);

                    if ((value as IConvertible) == null)
                    {
                        return(base.ConvertTo(context, culture, value, destinationType));
                    }

                    object  obj   = ((IConvertible)value).ToType(underlyingType, culture);
                    Type [] types = new Type[] {
                        typeof(Type),
                        underlyingType
                    };
                    MethodInfo methodInfo = typeof(Enum).GetMethod("ToObject", types);

                    if (methodInfo == null)
                    {
                        return(base.ConvertTo(context, culture, value, destinationType));
                    }

                    return(new InstanceDescriptor(methodInfo, new object[] { _type, obj }));
                }

                FieldInfo fieldInfo = _type.GetField(text1);
                if (fieldInfo != null)
                {
                    return(new InstanceDescriptor(fieldInfo, null));
                }
            }

            return(base.ConvertTo(context, culture, value, destinationType));
        }