Exemplo n.º 1
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;
            }
Exemplo n.º 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
                    {
                        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;
            }
Exemplo n.º 3
0
            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);
                        if (memberAttributes != null && memberAttributes.Length > 0)
                        {
                            if (memberAttributes.Length > 1)
                            {
                                ThrowInvalidDataContractException(SR.TooManyEnumMembers(DataContract.GetClrTypeFullName(field.DeclaringType), field.Name));
                            }
                            EnumMemberAttribute memberAttribute = (EnumMemberAttribute)memberAttributes[0];

                            DataMember memberContract = new DataMember(field);
                            // ALTERED_FOR_PORT: Don't have access to .IsSetExplicilty property
                            if (memberAttribute.Value != null && memberAttribute.Value.Length > 0)
                            {
                                memberContract.Name = memberAttribute.Value;
                            }
                            else
                            {
                                memberContract.Name = field.Name;
                            }
                            ClassDataContract.CheckAndAddMember(tempMembers, memberContract, memberValuesTable);
                            enumMemberValid = true;
                        }

                        object[] dataMemberAttributes = field.GetCustomAttributes(Globals.TypeOfDataMemberAttribute, false);
                        if (dataMemberAttributes != null && dataMemberAttributes.Length > 0)
                        {
                            ThrowInvalidDataContractException(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)((IConvertible)enumValue).ToUInt64(null));
                        }
                        else
                        {
                            tempValues.Add(((IConvertible)enumValue).ToInt64(null));
                        }
                    }
                }

                Thread.MemoryBarrier();
                members = tempMembers;
                values  = tempValues;
            }