Example #1
0
        public static void WalkTypes(List <Type> knownTypes)
        {
            Type newType = typeof(T);

            if (knownTypes.Contains(newType))
            {
                return;
            }
            knownTypes.Add(newType);
            foreach (Property <T> prop in writeProps)
            {
                bool dummy;
                Type propType   = prop.PropertyType,
                     actualType = Nullable.GetUnderlyingType(propType)
                                  ?? PropertyFactory.GetListType(propType, out dummy) ?? propType;

                if (Serializer.IsEntityType(actualType))
                {
                    typeof(Serializer <>)
                    .MakeGenericType(actualType)
                    .GetMethod("WalkTypes")
                    .Invoke(null, new object[] { knownTypes });
                }
                else if (actualType.IsEnum)
                {
                    if (!knownTypes.Contains(actualType))
                    {
                        knownTypes.Add(actualType);
                    }
                }
            }
        }
Example #2
0
            /// <summary>
            /// Can the given type be meaningfully with protobuf-net?
            /// </summary>
            public static bool CanSerialize(Type type)
            {
                if (type == null)
                {
                    throw new ArgumentNullException("type");
                }
                if (type.IsValueType)
                {
                    return(false);
                }

                // serialize as item?
                if (Serializer.IsEntityType(type))
                {
                    return(true);
                }

                // serialize as list?
                bool enumOnly;
                Type itemType = PropertyFactory.GetListType(type, out enumOnly);

                if (itemType != null &&
                    (!enumOnly || Serializer.HasAddMethod(type, itemType)) &&
                    Serializer.IsEntityType(itemType))
                {
                    return(true);
                }
                return(false);
            }