Ejemplo n.º 1
0
        /// <summary>
        /// Initializes default collection type definition and applies customizations (if any).
        /// </summary>
        public CollectionDefinition GetCollectionDefinition(TypeDefinition typeDefinition)
        {
            var collectionDefinition = new CollectionDefinition(typeDefinition.Type.MakeArrayType())
            {
                ItemDefinition    = typeDefinition,
                CanHoldNullValues = true,
                IsAnonymous       = true
            };

            schemaExporter.ExportTypeDefinition(collectionDefinition);

            return(collectionDefinition);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Initializes default type definition and applies customizations (if any).
        /// </summary>
        public TypeDefinition GetTypeDefinition(Type type, string typeName = null)
        {
            XName qualifiedName = null;

            if (type.IsArray)
            {
                if (!string.IsNullOrWhiteSpace(typeName))
                {
                    qualifiedName = XName.Get(typeName, ProtocolDefinition.ProducerNamespace);
                }

                var collectionDefinition = new CollectionDefinition(type)
                {
                    Name              = qualifiedName,
                    ItemDefinition    = GetTypeDefinition(type.GetElementType()),
                    IsAnonymous       = qualifiedName == null,
                    CanHoldNullValues = true
                };

                schemaExporter.ExportTypeDefinition(collectionDefinition);

                return(collectionDefinition);
            }

            var typeInfo      = type.GetTypeInfo();
            var typeAttribute = typeInfo.GetCustomAttribute <XmlTypeAttribute>();
            var isAnonymous   = typeAttribute != null && typeAttribute.AnonymousType;

            var normalizedType = type.NormalizeType();

            if (!isAnonymous)
            {
                qualifiedName = XName.Get((typeAttribute?.TypeName).GetValueOrDefault(typeName ?? normalizedType.Name),
                                          typeAttribute?.Namespace ?? ProtocolDefinition.ProducerNamespace);
            }

            var typeDefinition = new TypeDefinition(normalizedType)
            {
                ContentComparer       = PropertyComparer.Instance,
                HasStrictContentOrder = true,
                IsAnonymous           = isAnonymous,
                Name              = qualifiedName,
                State             = DefinitionState.Default,
                CanHoldNullValues = typeInfo.IsClass || normalizedType != type
            };

            schemaExporter.ExportTypeDefinition(typeDefinition);

            return(typeDefinition);
        }