Beispiel #1
0
        private static Entity Load(Type type)
        {
            Attribute[] attribs  = Attribute.GetCustomAttributes(type);
            bool        isEntity = false;
            string      name     = null;

            for (int i = 0; i < attribs.Length; i++)
            {
                if (attribs[i] is ProtoContractAttribute)
                {
                    ProtoContractAttribute pca = (ProtoContractAttribute)attribs[i];
                    name     = pca.Name;
                    isEntity = true;
                    break;
                }
            }

            if (!isEntity)
            {
                for (int i = 0; i < attribs.Length; i++)
                {
                    if (attribs[i] is XmlTypeAttribute)
                    {
                        XmlTypeAttribute xta = (XmlTypeAttribute)attribs[i];
                        name     = xta.TypeName;
                        isEntity = true;
                        break;
                    }
                }
            }
            return(isEntity ? new Entity(type, name, null) : null);
        }
Beispiel #2
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();
        }
Beispiel #3
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);
        }