Ejemplo n.º 1
0
        private Enum GetUndefinedValue()
        {
            var undefinedEnumValueAttribute = AttributeUtility.GetCustomAttribute <UndefinedEnumValueAttribute> (UnderlyingType, false);

            if (undefinedEnumValueAttribute == null)
            {
                return(null);
            }

            if (!UnderlyingType.IsInstanceOfType(undefinedEnumValueAttribute.GetValue()))
            {
                throw new InvalidOperationException(
                          string.Format(
                              "The enum type '{0}' defines a '{1}' with an enum value that belongs to a different enum type.",
                              UnderlyingType,
                              typeof(UndefinedEnumValueAttribute)));
            }

            if (IsNullable)
            {
                throw new InvalidOperationException(
                          string.Format(
                              "The property '{0}' defined on type '{1}' must not be nullable since the property's type already defines a '{2}'.",
                              Identifier,
                              PropertyInfo.DeclaringType,
                              typeof(UndefinedEnumValueAttribute)));
            }

            return(undefinedEnumValueAttribute.GetValue());
        }
Ejemplo n.º 2
0
        protected override bool SetValueInternal(object value, ValueUpdateSource updateSource)
        {
            bool result = false;

#if _DOTNET2
            NullableConverter nc = TypeConverter as NullableConverter;
            if ((value == null) || ((value != null) && UnderlyingType.IsInstanceOfType(value)) ||
                ((nc != null) && ((value == null) || nc.UnderlyingType.IsInstanceOfType(value))))
#else
            if ((value != null) && UnderlyingType.IsInstanceOfType(value))
#endif
            {
                UnderlyingValue = value;
                result          = true;
            }

            if (updateSource != ValueUpdateSource.FromChild)
            {
                if ((mOwnerEnumerator != null) &&   // mOwnerEnumerator could be null for the boolean of Property._enabledVariable
                    !mOwnerEnumerator.Property.Value.NoLinkWithChildren)
                {
                    PropertyTypeDescriptorContext context = GetTypeDescriptorContext(OwnerEnumerator);
                    if (TypeConverter.GetPropertiesSupported(context) == false)
                    {
                        PropertyEnumerator childEnum = mOwnerEnumerator.Children;
                        if (childEnum.Count > 0)
                        {
                            string masterStr = mOwnerEnumerator.Property.Value.GetStringValue();

                            char separator = mOwnerEnumerator.Property.Value.GroupedValueSeparator;
                            while (childEnum != childEnum.RightBound)
                            {
                                masterStr.TrimStart(null);

                                int count = masterStr.IndexOf(separator);

                                if (count != -1)
                                {
                                    childEnum.Property.Value.SetValue(masterStr.Substring(0, count));
                                    if (count + 2 < masterStr.Length)
                                    {
                                        masterStr = masterStr.Substring(count + 2); // advance after the separator and the leading space
                                    }
                                }
                                else
                                {
                                    childEnum.Property.Value.SetValue(masterStr);
                                    break;
                                }

                                childEnum.MoveNext();
                            }
                        }
                    }
                }
            }

            return(result);
        }