Exemple #1
0
        public MessagePart(MemberInfo member, MessagePartAttribute attribute)
        {
            this.field               = member as FieldInfo;
            this.property            = member as PropertyInfo;
            this.Name                = attribute.Name ?? member.Name;
            this.RequiredProtection  = attribute.RequiredProtection;
            this.IsRequired          = attribute.IsRequired;
            this.AllowEmpty          = attribute.AllowEmpty;
            this.IsSecuritySensitive = attribute.IsSecuritySensitive;
            this.memberDeclaredType  = (this.field != null) ? this.field.FieldType : this.property.PropertyType;
            this.defaultMemberValue  = DeriveDefaultValue(this.memberDeclaredType);
            Assumes.True(this.memberDeclaredType != null);
            if (attribute.Encoder == null)
            {
                if (!converters.TryGetValue(this.memberDeclaredType, out this.converter))
                {
                    if (this.memberDeclaredType.IsGenericType &&
                        this.memberDeclaredType.GetGenericTypeDefinition() == typeof(Nullable))
                    {
                        Type         underlyingType = Nullable.GetUnderlyingType(this.memberDeclaredType);
                        ValueMapping underlyingMapping;
                        if (converters.TryGetValue(underlyingType, out underlyingMapping))
                        {
                            this.converter = new ValueMapping(
                                underlyingMapping.ValueToString,
                                null,
                                str => str != null?underlyingMapping.StringToValue(str):null
                                );
                        }
                        else
                        {
                            this.converter = GetDefaultEncoder(underlyingType);
                        }
                    }
                    else
                    {
                        this.converter = GetDefaultEncoder(this.memberDeclaredType);
                    }
                }
            }
            else
            {
                this.converter = new ValueMapping(GetEncoder(attribute.Encoder));
            }
            FieldAttributes constAttributes = FieldAttributes.Static | FieldAttributes.Literal | FieldAttributes.HasDefault;

            if (this.field != null && (
                    (this.field.Attributes & FieldAttributes.InitOnly) == FieldAttributes.InitOnly ||
                    (this.field.Attributes & constAttributes) == constAttributes))
            {
                this.IsConstantValue = true;
                this.IsConstantValueAvailableStatically = this.field.IsStatic;
            }
            else if (this.property != null && !this.property.CanWrite)
            {
                this.IsConstantValue = true;
            }
            this.ValidateSettings();
        }
        private void ReflectMessageType()
        {
            this.mapping = new Dictionary <string, MessagePart>();
            Type currentType = this.MessageType;

            do
            {
                foreach (MemberInfo member in currentType.GetMembers(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly))
                {
                    if (member is PropertyInfo || member is FieldInfo)
                    {
                        MessagePartAttribute partAttribute =
                            (from a in member.GetCustomAttributes(typeof(MessagePartAttribute), true).OfType <MessagePartAttribute>()
                             orderby a.MinVersionValue descending
                             where a.MinVersionValue <= this.MessageVersion
                             where a.MaxVersionValue >= this.MessageVersion
                             select a).FirstOrDefault();
                        if (partAttribute != null)
                        {
                            MessagePart part = new MessagePart(member, partAttribute);
                            if (this.Mapping.ContainsKey(part.Name))
                            {
                                Logger.Messaging.WarnFormat(
                                    "Message type {0} has more than one message part named {1}.  Inherited members will be hidden.",
                                    this.MessageType.Name,
                                    part.Name
                                    );
                            }
                            else
                            {
                                this.mapping.Add(part.Name, part);
                            }
                        }
                    }
                }
                currentType = currentType.BaseType;
            } while (currentType != null);
            BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public;

            this.Constructors = this.MessageType.GetConstructors(flags);
        }