Example #1
0
        private string GetMemberControlWord(MemberInfo member, Type memberType, object memberValue, string attributeControlWord)
        {
            if (memberType == null)
            {
                throw (new ArgumentNullException("Type cannot be null"));
            }

            if (memberType.IsEnum)
            {
                throw (new ArgumentException("Type cannot be enum"));
            }

            string controlWord = "\\";

            if (!String.IsNullOrEmpty(attributeControlWord))
            {
                controlWord += attributeControlWord;
            }
            else
            {
                controlWord += member.Name.ToLower();
            }

            if (memberType.IsPrimitive)
            {
                if (memberType == typeof(int))
                {
                    RtfAttributeInfo info = RtfDocumentInfo.GetAttributeInfo(member);

                    if (info.IndexAttribute != null)
                    {
                        if ((int)memberValue >= 0)
                        {
                            controlWord += memberValue.ToString();
                        }
                        else
                        {
                            controlWord = String.Empty;
                        }
                    }
                    else
                    {
                        controlWord += memberValue.ToString();
                    }
                }
                else if (memberType != typeof(bool))
                {
                    controlWord += memberValue.ToString();
                }
                else if (!(bool)memberValue)
                {
                    controlWord = String.Empty;
                }
            }

            return(controlWord);
        }
Example #2
0
        private void Reflect(object instance, int arrayIndex)
        {
            Type type = instance.GetType();

            RtfAttributeInfo info = RtfDocumentInfo.GetAttributeInfo(type);

            string
                controlWord            = info.ControlWordAttribute != null ? "\\" + info.ControlWordAttribute.Name : String.Empty,
                controlWordDenotingEnd = info.ControlWordDenotingEndAttribute != null ? "\\" + info.ControlWordDenotingEndAttribute.Name : String.Empty;

            bool
                isIndexed          = info.ControlWordAttribute != null && info.ControlWordAttribute.IsIndexed,
                enclosedWithBraces = info.EnclosingBracesAttribute != null && info.EnclosingBracesAttribute.Braces,
                closingSemicolon   = info.EnclosingBracesAttribute != null && info.EnclosingBracesAttribute.ClosingSemicolon;

            if (enclosedWithBraces)
            {
                Append('{');
            }

            if (!String.IsNullOrEmpty(controlWord))
            {
                Append(controlWord);

                if (isIndexed && arrayIndex >= 0)
                {
                    Append(arrayIndex.ToString());
                }
            }

            MemberInfo[] members = RtfDocumentInfo.GetTypeMembers(type);

            foreach (MemberInfo member in members)
            {
                ReflectMember(member, instance);
            }

            if (closingSemicolon)
            {
                Append(';');
            }

            if (!String.IsNullOrEmpty(controlWordDenotingEnd))
            {
                Append(controlWordDenotingEnd);
            }

            if (enclosedWithBraces)
            {
                Append('}');
            }
        }
Example #3
0
        private void ReflectMember(MemberInfo member, object instance)
        {
            RtfAttributeInfo info = RtfDocumentInfo.GetAttributeInfo(member);

            if (!info.HasAttributes)
            {
                return;
            }

            if (info.IgnoreAttribute != null)
            {
                return;
            }

            bool include = true;

            if (info.IncludeAttribute != null)
            {
                include = GetCondition(instance, info.IncludeAttribute);
            }

            if (!include)
            {
                return;
            }

            Type   memberType = GetMemberType(member);
            object value      = GetMemberValue(member, instance);

            if (info.ControlWordAttribute != null)
            {
                Append(GetControlWord(member, memberType, value, info.ControlWordAttribute.Name));

                if (memberType.IsClass)
                {
                    if (TypeIsEnumerable(memberType))
                    {
                        ReflectArray(memberType, value);
                    }
                    else
                    {
                        Reflect(value);
                    }
                }

                return;
            }

            if (info.TextDataAttribute != null)
            {
                Append(' ');

                if (info.TextDataAttribute.Quotes)
                {
                    Append('"');
                }

                Append(EncodeText(value.ToString(), info.TextDataAttribute.TextDataType));

                if (info.TextDataAttribute.Quotes)
                {
                    Append('"');
                }

                return;
            }

            if (info.ControlGroupAttribute != null)
            {
                AppendLine();
                Append('{');
                Append('\\');
                Append(info.ControlGroupAttribute.Name);
                Append(' ');

                ReflectArray(memberType, value);

                Append('}');
                AppendLine();

                return;
            }

            if (info.IncludeAttribute != null && memberType.IsClass)
            {
                if (TypeIsEnumerable(memberType))
                {
                    ReflectArray(memberType, value);
                }
                else if (value != null)
                {
                    Reflect(value);
                }
            }
        }
Example #4
0
        private string GetEnumControlWord(MemberInfo member, Type memberType, object memberValue, string attributeControlWord)
        {
            RtfAttributeInfo info = RtfDocumentInfo.GetAttributeInfo(memberType);

            if (memberType == null)
            {
                throw (new ArgumentNullException("Type cannot be null"));
            }

            if (!memberType.IsEnum)
            {
                throw (new ArgumentException("Type cannot be enum"));
            }

            if (info.EnumAsControlWordAttribute == null)
            {
                return(String.Empty);
            }

            string controlWord = "\\";

            if (!String.IsNullOrEmpty(attributeControlWord))
            {
                controlWord += attributeControlWord;
            }
            else
            {
                controlWord += info.EnumAsControlWordAttribute.Prefix;
            }

            switch (info.EnumAsControlWordAttribute.Conversion)
            {
            case RtfEnumConversion.UseAttribute:
            {
                MemberInfo[] enumMember = memberType.GetMember(((Enum)memberValue).ToString());

                if (enumMember != null && enumMember.Length > 0)
                {
                    RtfAttributeInfo enumMemberInfo = RtfDocumentInfo.GetAttributeInfo(enumMember[0]);

                    if (enumMemberInfo.ControlWordAttribute != null)
                    {
                        controlWord += enumMemberInfo.ControlWordAttribute.Name;
                    }
                }
                break;
            }

            case RtfEnumConversion.UseValue:
            {
                if (memberValue == null)
                {
                    throw (new ArgumentNullException(String.Format("Type {0} cannot take null as a value", memberType.Name)));
                }

                controlWord += ((int)memberValue).ToString();
                break;
            }

            case RtfEnumConversion.UseName:
            default:
            {
                controlWord += ((Enum)memberValue).ToString().ToLower();
                break;
            }
            }

            return(controlWord);
        }