Ejemplo n.º 1
0
        internal static IEnumerable <ProtoEnumValue <TEnum> > GetEnumValues <TEnum>()
        {
            List <ProtoEnumValue <TEnum> > list = new List <ProtoEnumValue <TEnum> >();

            foreach (FieldInfo enumField in typeof(TEnum).GetFields(BindingFlags.Static | BindingFlags.Public))
            {
                if (!enumField.IsLiteral)
                {
                    continue;
                }

                TEnum key             = (TEnum)enumField.GetValue(null);
                ProtoEnumAttribute ea = AttributeUtils.GetAttribute <ProtoEnumAttribute>(enumField);
                int    value;
                string name = (ea == null || string.IsNullOrEmpty(ea.Name)) ? enumField.Name : ea.Name;

                if (ea == null || !ea.HasValue())
                {
                    value = (int)Convert.ChangeType(key, typeof(int), CultureInfo.InvariantCulture);
                }
                else
                {
                    value = (int)ea.Value;
                }

                list.Add(new ProtoEnumValue <TEnum>(key, value, name));
            }
            list.Sort(delegate(ProtoEnumValue <TEnum> x, ProtoEnumValue <TEnum> y)
            {
                return(x.WireValue.CompareTo(y.WireValue));
            });
            return(list);
        }
Ejemplo n.º 2
0
        public static void AppendProto(StringBuilder sb, int nestLevel)
        {
            string descText, name = Serializer.GetDefinedTypeName <T>();

            sb.AppendLine();
            Indent(sb, nestLevel).Append("message ").Append(name).Append(" {");

            DescriptionAttribute desc = AttributeUtils.GetAttribute <DescriptionAttribute>(typeof(T));

            descText = desc == null ? null : desc.Description;
            if (!string.IsNullOrEmpty(descText))
            {
                sb.Append(" //").Append(descText); // TODO: remove crlf
            }

            sb.AppendLine();
            nestLevel++;
            for (int i = 0; i < writeProps.Length; i++)
            {
                Property <T> prop = writeProps[i];
                Indent(sb, nestLevel).Append(' ')
                .Append(prop.IsRepeated ? "repeated" :
                        (prop.IsOptional ? "optional" : "required"))
                .Append(prop.IsGroup ? " group " : " ")
                .Append(prop.DefinedType).Append(' ')
                .Append(prop.Name).Append(" = ").Append(prop.Tag);

                object def = prop.DefaultValue;
                if (def != null)
                {
                    string defText = Convert.ToString(def, CultureInfo.InvariantCulture);
                    if (prop.DefinedType == ProtoFormat.STRING)
                    {
                        // note; waiting on clarification over quote escape rules
                        sb.Append(" [default = \"").Append(defText).Append("\"]");
                    }
                    else
                    {
                        sb.Append(" [default = ").Append(defText).Append("]");
                    }
                }

                sb.Append(";");
                descText = prop.Description;
                if (!string.IsNullOrEmpty(descText))
                {
                    sb.Append(" //").Append(descText); // TODO: remove crlf
                }

                sb.AppendLine();
            }

            nestLevel--;
            Indent(sb, nestLevel).AppendLine("}");
        }
Ejemplo n.º 3
0
        public static void AppendEnum <TEnum>(StringBuilder sb, int nestLevel) where TEnum : struct
        {
            ProtoContractAttribute attrib = AttributeUtils.GetAttribute <ProtoContractAttribute>(typeof(TEnum));
            string name = attrib == null || string.IsNullOrEmpty(attrib.Name) ? typeof(TEnum).Name : attrib.Name;

            Indent(sb, nestLevel).Append("enum ").Append(name).Append(" {").AppendLine();
            foreach (Serializer.ProtoEnumValue <TEnum> value in Serializer.GetEnumValues <TEnum>())
            {
                Indent(sb, nestLevel + 1).Append(' ').Append(value.Name)
                .Append(" = ").Append(value.WireValue).Append(";").AppendLine();
            }
            Indent(sb, nestLevel).Append("}").AppendLine();
        }
Ejemplo n.º 4
0
        internal static bool TryGetTag(MemberInfo member, out int tag, out string name, bool callerIsTagInference, out DataFormat format, out MemberSerializationOptions options)
        {
            name    = member.Name;
            format  = DataFormat.Default;
            tag     = -1;
            options = MemberSerializationOptions.None;

            // check for delegates (don't even try!)
            Type valueType;

            switch (member.MemberType)
            {
            case MemberTypes.Property:
                valueType = ((PropertyInfo)member).PropertyType;
                break;

            case MemberTypes.Field:
                valueType = ((FieldInfo)member).FieldType;
                break;

            default:     // not sure what this is!
                return(false);
            }
            if (valueType.IsSubclassOf(typeof(Delegate)))
            {
                return(false);
            }

            // check for exclusion
            if (AttributeUtils.GetAttribute <ProtoIgnoreAttribute>(member) != null ||
                AttributeUtils.GetAttribute <ProtoPartialIgnoreAttribute>(member.ReflectedType,
                                                                          delegate(ProtoPartialIgnoreAttribute ppia)
                                                                          { return(ppia.MemberName == member.Name); }) != null)
            {
                return(false);
            }

            // check against the property
            ProtoMemberAttribute pm = AttributeUtils.GetAttribute <ProtoMemberAttribute>(member);

            if (pm == null)
            { // check also against the type
                pm = AttributeUtils.GetAttribute <ProtoPartialMemberAttribute>(member.ReflectedType,
                                                                               delegate(ProtoPartialMemberAttribute ppma) { return(ppma.MemberName == member.Name); });
            }
            if (pm != null)
            {
                format = pm.DataFormat;
                if (!string.IsNullOrEmpty(pm.Name))
                {
                    name = pm.Name;
                }
                tag     = pm.Tag;
                options = pm.Options;
                return(tag > 0);
            }

            ProtoContractAttribute pca = AttributeUtils.GetAttribute <ProtoContractAttribute>(member.DeclaringType);

            if (pca != null && pca.ImplicitFields != ImplicitFields.None)
            {
                if (callerIsTagInference)
                {
                    return(true);                     // short-circuit
                }
                List <MemberInfo> members = new List <MemberInfo>();
                switch (pca.ImplicitFields)
                {
                case ImplicitFields.AllFields:
                    AddImplicitByDeclaringType(member.DeclaringType, members,
                                               member.DeclaringType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic));
                    break;

                case ImplicitFields.AllPublic:
                    AddImplicitByDeclaringType(member.DeclaringType, members,
                                               member.DeclaringType.GetFields(BindingFlags.Instance | BindingFlags.Public));
                    AddImplicitByDeclaringType(member.DeclaringType, members,
                                               member.DeclaringType.GetProperties(BindingFlags.Instance | BindingFlags.Public));
                    break;

                default:
                    throw new NotSupportedException("Unknown ImplicitFields option: " + pca.ImplicitFields);
                }
                members.Sort(delegate(MemberInfo x, MemberInfo y)
                {
                    return(string.CompareOrdinal(x.Name, y.Name));
                });
                int index = members.IndexOf(member);
                if (index >= 0)
                {
                    tag = index + pca.ImplicitFirstTag;
                    return(true);
                }
                return(false);
            }


            XmlElementAttribute xe = AttributeUtils.GetAttribute <XmlElementAttribute>(member);

            if (xe != null)
            {
                if (!string.IsNullOrEmpty(xe.ElementName))
                {
                    name = xe.ElementName;
                }
                tag = xe.Order;
                return(tag > 0);
            }

            XmlArrayAttribute xa = AttributeUtils.GetAttribute <XmlArrayAttribute>(member);

            if (xa != null)
            {
                if (!string.IsNullOrEmpty(xa.ElementName))
                {
                    name = xa.ElementName;
                }
                tag = xa.Order;
                return(tag > 0);
            }

            return(false);
        }