public static void Register(RuntimeTypeModel typeModel, Assembly assembly)
        {
            foreach (var type in GetTypesSafely(assembly))
            {
                if (type.IsClass == false && type.IsValueType == false)
                    continue;

                if (Attribute.GetCustomAttribute(type, typeof(ProtoBuf.ProtoContractAttribute)) == null)
                    continue;

                var loweredTypeName = type.Name.ToLower();
                if (loweredTypeName.Contains("surrogatedirectives"))
                {
                    foreach (var field in type.GetFields())
                    {
                        var sourceType = FindSurrogateSourceType(field.FieldType);
                        if (sourceType != null)
                        {
                            if (typeModel.CanSerialize(sourceType))
                                continue;
                            try
                            {
                                typeModel.Add(sourceType, false).SetSurrogate(field.FieldType);
                            }
                            catch (InvalidOperationException)
                            {
                            }
                        }
                    }
                }
                else if (type.Name.ToLower().Contains("surrogate"))
                {
                    var sourceType = FindSurrogateSourceType(type);
                    if (sourceType != null)
                    {
                        if (typeModel.CanSerialize(sourceType))
                            continue;
                        try
                        {
                            typeModel.Add(sourceType, false).SetSurrogate(type);
                        }
                        catch (InvalidOperationException)
                        {
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        internal static void Build(RuntimeTypeModel runtimeTypeModel, Type type)
        {
            if (BuiltTypes.Contains(type))
            {
                return;
            }

            lock (type)
            {
                if (runtimeTypeModel.CanSerialize(type))
                {
                    if (type.IsGenericType)
                    {
                        BuildGenerics(runtimeTypeModel, type);
                    }
                    return;
                }

                var meta   = runtimeTypeModel.Add(type, false);
                var fields = type.GetFields(Flags);

                meta.Add(fields.Select(x => x.Name).ToArray());
                meta.UseConstructor = false;

                BuildBaseClasses(runtimeTypeModel, type);
                BuildGenerics(runtimeTypeModel, type);

                foreach (var memberType in fields.Select(f => f.FieldType).Where(t => !t.IsPrimitive))
                {
                    Build(runtimeTypeModel, memberType);
                }

                BuiltTypes.Add(type);
            }
        }