// can not use generic, at it will emit error
        // Compiler Error CS0310
        // The type 'typename' must have a public parameterless constructor in order to use it as parameter 'parameter' in the generic type or method 'generic'

        internal sealed override OpenXmlPart NewPart(string relationshipType, string contentType)
        {
            ThrowIfObjectDisposed();

            if (contentType == null)
            {
                throw new ArgumentNullException(nameof(contentType));
            }

            if (PartConstraints.TryGetValue(relationshipType, out var partConstraintRule))
            {
                if (!partConstraintRule.MaxOccursGreatThanOne)
                {
                    if (GetSubPart(relationshipType) != null)
                    {
                        // already have one, can not add new one.
                        throw new InvalidOperationException(ExceptionMessages.OnlyOnePartAllowed);
                    }
                }

                OpenXmlPart child = CreateOpenXmlPart(relationshipType);

                child.CreateInternal(OpenXmlPackage, this, contentType, null);

                // add it and get the id
                string relationshipId = AttachChild(child);

                ChildrenParts.Add(relationshipId, child);

                return(child);
            }
            throw new ArgumentOutOfRangeException(nameof(relationshipType));
        }
Exemple #2
0
        /// <summary>
        /// Init a new created part
        /// </summary>
        /// <typeparam name="T">The type of the part, must be derived from OpenXmlPart.</typeparam>
        /// <param name="newPart">The part to be initialized.</param>
        /// <param name="contentType">The content type of the part.</param>
        /// <param name="id">The relationship id.</param>
        internal override void InitPart <T>(T newPart, string contentType, string id)
        {
            ThrowIfObjectDisposed();

            if (contentType == null)
            {
                throw new ArgumentNullException(nameof(contentType));
            }

            if (contentType.Length == 0)
            {
                throw new ArgumentException(ExceptionMessages.StringArgumentEmptyException, nameof(contentType));
            }

            newPart.CreateInternal(InternalOpenXmlPackage, ThisOpenXmlPart, contentType, null);

            string relationshipId = AttachChild(newPart, id);

            ChildrenParts.Add(relationshipId, newPart);

            return;
        }