Ejemplo n.º 1
0
        private void Add(string typename,
                         Type type,
                         int typeId,
                         int?baseTypeId,
                         int?surrogateId,
                         int?underlyingEnumTypeId,
                         TypeClassification classification,
                         IEnumerable <FieldDescription> fields)
        {
            var baseTypeDescription           = baseTypeId != null ? _typeDescriptionsById[baseTypeId.Value] : null;
            var underlyingEnumTypeDescription = underlyingEnumTypeId != null ? _typeDescriptionsById[underlyingEnumTypeId.Value] : null;

            var typeDescription = TypeDescription.Create(type,
                                                         typename,
                                                         typeId,
                                                         baseTypeDescription,
                                                         underlyingEnumTypeDescription,
                                                         classification,
                                                         fields);

            AddTypeDescription(typeDescription);

            if (surrogateId != null)
            {
                var surrogateDescription = GetTypeDescription(surrogateId.Value);
                typeDescription.SurrogateType       = surrogateDescription;
                surrogateDescription.SurrogatedType = typeDescription;
            }
        }
Ejemplo n.º 2
0
        private TypeDescription AddType(Type type)
        {
            var baseTypeDescription           = AddBaseTypeOf(type);
            var underlyingEnumTypeDescription = AddEnumTypeOf(type);

            var fields      = FieldDescription.FindSerializableFields(type);
            var properties  = FieldDescription.FindSerializableProperties(type);
            var enumMembers = FieldDescription.FindSerializableEnumMembers(type);
            var members     = CreateFieldDescriptions(fields, properties, enumMembers, baseTypeDescription, underlyingEnumTypeDescription);

            if (type.IsArray && type.GetArrayRank() == 1)
            {
                Add(type.GetElementType());
            }

            if (type.IsGenericType)
            {
                foreach (var argument in type.GenericTypeArguments)
                {
                    Add(argument);
                }
            }

            var id = _nextId;

            ++_nextId;
            var typeDescription = TypeDescription.Create(type, id,
                                                         baseTypeDescription,
                                                         underlyingEnumTypeDescription,
                                                         members,
                                                         hasSurrogate: HasSurrogate(type));

            AddTypeDescription(typeDescription);

            var surrogateAttribute = type.GetCustomAttribute <DataContractSurrogateForAttribute>();

            if (surrogateAttribute != null)
            {
                if (surrogateAttribute.ActualType == null)
                {
                    throw new NotImplementedException();
                }

                var surrogatedType = surrogateAttribute.ActualType;

                var surrogatedDescription = Add(surrogatedType);
                surrogatedDescription.SurrogateType = typeDescription;
                typeDescription.SurrogatedType      = surrogatedDescription;
            }

            return(typeDescription);
        }