Example #1
0
        public EnumMap(
            Type type, QName qname, KnownTypeCollection knownTypes)
            : base(type, qname, knownTypes)
        {
            bool has_dc = false;

            object [] atts = RuntimeType.GetCustomAttributes(
                typeof(DataContractAttribute), false);
            if (atts.Length != 0)
            {
                has_dc = true;
            }
            flag_attr = type.GetCustomAttributes(typeof(FlagsAttribute), false).Length > 0;

            enum_members = new List <EnumMemberInfo> ();
            BindingFlags flags = BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Static;

            foreach (FieldInfo fi in RuntimeType.GetFields(flags))
            {
                string name = fi.Name;
                if (has_dc)
                {
                    EnumMemberAttribute ema =
                        GetEnumMemberAttribute(fi);
                    if (ema == null)
                    {
                        continue;
                    }

                    if (ema.Value != null)
                    {
                        name = ema.Value;
                    }
                }

                enum_members.Add(new EnumMemberInfo(name, fi.GetValue(null)));
            }
        }
Example #2
0
            private void ImportDataMembers()
            {
                Type type = this.UnderlyingType;

                FieldInfo[] fields = type.GetFields(BindingFlags.Static | BindingFlags.Public);
                Dictionary <string, DataMember> memberValuesTable = new Dictionary <string, DataMember>();
                List <DataMember> tempMembers = new List <DataMember>(fields.Length);
                List <long>       tempValues  = new List <long>(fields.Length);

                for (int i = 0; i < fields.Length; i++)
                {
                    FieldInfo field           = fields[i];
                    bool      enumMemberValid = false;
                    if (_hasDataContract)
                    {
                        object[] memberAttributes = field.GetCustomAttributes(Globals.TypeOfEnumMemberAttribute, false).ToArray();
                        if (memberAttributes != null && memberAttributes.Length > 0)
                        {
                            if (memberAttributes.Length > 1)
                            {
                                ThrowInvalidDataContractException(SR.Format(SR.TooManyEnumMembers, DataContract.GetClrTypeFullName(field.DeclaringType !), field.Name));
                            }
                            EnumMemberAttribute memberAttribute = (EnumMemberAttribute)memberAttributes[0];

                            DataMember memberContract = new DataMember(field);
                            if (memberAttribute.IsValueSetExplicitly)
                            {
                                if (memberAttribute.Value == null || memberAttribute.Value.Length == 0)
                                {
                                    ThrowInvalidDataContractException(SR.Format(SR.InvalidEnumMemberValue, field.Name, DataContract.GetClrTypeFullName(type)));
                                }
                                memberContract.Name = memberAttribute.Value;
                            }
                            else
                            {
                                memberContract.Name = field.Name;
                            }
                            ClassDataContract.CheckAndAddMember(tempMembers, memberContract, memberValuesTable);
                            enumMemberValid = true;
                        }

                        object[] dataMemberAttributes = field.GetCustomAttributes(Globals.TypeOfDataMemberAttribute, false).ToArray();
                        if (dataMemberAttributes != null && dataMemberAttributes.Length > 0)
                        {
                            ThrowInvalidDataContractException(SR.Format(SR.DataMemberOnEnumField, DataContract.GetClrTypeFullName(field.DeclaringType !), field.Name));
                        }
                    }
                    else
                    {
                        if (!field.IsNotSerialized)
                        {
                            DataMember memberContract = new DataMember(field);
                            memberContract.Name = field.Name;
                            ClassDataContract.CheckAndAddMember(tempMembers, memberContract, memberValuesTable);
                            enumMemberValid = true;
                        }
                    }

                    if (enumMemberValid)
                    {
                        object?enumValue = field.GetValue(null);
                        if (_isULong)
                        {
                            tempValues.Add((long)Convert.ToUInt64(enumValue, null));
                        }
                        else
                        {
                            tempValues.Add(Convert.ToInt64(enumValue, null));
                        }
                    }
                }

                Interlocked.MemoryBarrier();
                _members = tempMembers;
                _values  = tempValues;
            }
 public void MatchesDescription_FieldNameAndDescriptDiffer_False()
 {
     var attribute = new EnumMemberAttribute { Value = "bad value" };
     Assert.False(attribute.MatchesDescription("won't match"));
 }
 public void MatchesDescription_EmptyFieldNameNonNullDescript_False()
 {
     var attribute = new EnumMemberAttribute { Value = string.Empty };
     Assert.False(attribute.MatchesDescription(MyEnumMemberValue));
 }
 public void MatchesDescription_WhiteSpacePaddedConstantNameUsed_True()
 {
     var attribute = new EnumMemberAttribute { Value = MyEnumMemberValue };
     Assert.True(attribute.MatchesDescription("  " + MyEnumMemberValue + "\t\r\t  "));
 }
 public void MatchesDescription_NameCaseDiffers_True()
 {
     var attribute = new EnumMemberAttribute { Value = MyEnumMemberValue };
     Assert.True(attribute.MatchesDescription(MyEnumMemberValue.ToUpper()));
 }
 public void MatchesDescription_ConstantNameUsed_True()
 {
     var attribute = new EnumMemberAttribute {Value = MyEnumMemberValue};
     Assert.True(attribute.MatchesDescription(MyEnumMemberValue));
 }
Example #8
0
            private void ImportDataMembers()
            {
                Type underlyingType = base.UnderlyingType;

                FieldInfo[] fields = underlyingType.GetFields(BindingFlags.Public | BindingFlags.Static);
                Dictionary <string, DataMember> memberNamesTable = new Dictionary <string, DataMember>();
                List <DataMember> members = new List <DataMember>(fields.Length);
                List <long>       list2   = new List <long>(fields.Length);

                for (int i = 0; i < fields.Length; i++)
                {
                    FieldInfo memberInfo = fields[i];
                    bool      flag       = false;
                    if (this.hasDataContract)
                    {
                        object[] customAttributes = memberInfo.GetCustomAttributes(Globals.TypeOfEnumMemberAttribute, false);
                        if ((customAttributes != null) && (customAttributes.Length > 0))
                        {
                            if (customAttributes.Length > 1)
                            {
                                base.ThrowInvalidDataContractException(System.Runtime.Serialization.SR.GetString("TooManyEnumMembers", new object[] { DataContract.GetClrTypeFullName(memberInfo.DeclaringType), memberInfo.Name }));
                            }
                            EnumMemberAttribute attribute      = (EnumMemberAttribute)customAttributes[0];
                            DataMember          memberContract = new DataMember(memberInfo);
                            if (attribute.IsValueSetExplicit)
                            {
                                if ((attribute.Value == null) || (attribute.Value.Length == 0))
                                {
                                    base.ThrowInvalidDataContractException(System.Runtime.Serialization.SR.GetString("InvalidEnumMemberValue", new object[] { memberInfo.Name, DataContract.GetClrTypeFullName(underlyingType) }));
                                }
                                memberContract.Name = attribute.Value;
                            }
                            else
                            {
                                memberContract.Name = memberInfo.Name;
                            }
                            ClassDataContract.CheckAndAddMember(members, memberContract, memberNamesTable);
                            flag = true;
                        }
                        object[] objArray2 = memberInfo.GetCustomAttributes(Globals.TypeOfDataMemberAttribute, false);
                        if ((objArray2 != null) && (objArray2.Length > 0))
                        {
                            base.ThrowInvalidDataContractException(System.Runtime.Serialization.SR.GetString("DataMemberOnEnumField", new object[] { DataContract.GetClrTypeFullName(memberInfo.DeclaringType), memberInfo.Name }));
                        }
                    }
                    else if (!memberInfo.IsNotSerialized)
                    {
                        DataMember member2 = new DataMember(memberInfo)
                        {
                            Name = memberInfo.Name
                        };
                        ClassDataContract.CheckAndAddMember(members, member2, memberNamesTable);
                        flag = true;
                    }
                    if (flag)
                    {
                        object obj2 = memberInfo.GetValue(null);
                        if (this.isULong)
                        {
                            list2.Add((long)((IConvertible)obj2).ToUInt64(null));
                        }
                        else
                        {
                            list2.Add(((IConvertible)obj2).ToInt64(null));
                        }
                    }
                }
                Thread.MemoryBarrier();
                this.members = members;
                this.values  = list2;
            }