question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Consider using extension methods for adding extended parts to existing parts

See original GitHub issue

#1387 added a bunch of methods to all the parts that are essentially doing the same thing. This should probably be reworked to be a single extension method, i.e.

public static class EmbeddedPackageExtensions
{
        /// <summary>
        /// Adds a EmbeddedPackagePart to the part
        /// </summary>
        /// <param name="contentType">The content type of the EmbeddedPackagePart</param>
        /// <param name="id">The relationship id</param>
        /// <return>The newly added part</return>
        public static EmbeddedPackagePart AddEmbeddedPackagePart(this OpenXmlPart part, string contentType, string id)
        {
            var childPart = new EmbeddedPackagePart();
            part.InitPart(childPart, contentType, id);
            return childPart;
        }

        /// <summary>
        /// Adds a EmbeddedPackagePart
        /// </summary>
        /// <param name="partType">The part type of the EmbeddedPackagePart</param>
        /// <param name="id">The relationship id</param>
        /// <return>The newly added part</return>
        public static EmbeddedPackagePart AddEmbeddedPackagePart(this OpenXmlPart part, EmbeddedPackagePartType partType, string id)
        {
            var contentType = EmbeddedPackagePartTypeInfo.GetContentType(partType);
            var partExtension = EmbeddedPackagePartTypeInfo.GetTargetExtension(partType);
            part.Features.GetRequired<IPartExtensionFeature>().Register(contentType, partExtension);
            return part.AddEmbeddedPackagePart(contentType, id);
        }

        /// <summary>
        /// Adds a EmbeddedPackagePart
        /// </summary>
        /// <param name="partType">The part type of the EmbeddedPackagePart</param>
        /// <return>The newly added part</return>
        public static EmbeddedPackagePart AddEmbeddedPackagePart(this OpenXmlPart part, EmbeddedPackagePartType partType)
        {
            var contentType = EmbeddedPackagePartTypeInfo.GetContentType(partType);
            var partExtension = EmbeddedPackagePartTypeInfo.GetTargetExtension(partType);
            part.Features.GetRequired<IPartExtensionFeature>().Register(contentType, partExtension);
            return part.AddEmbeddedPackagePart(contentType);
        }
}

Issue Analytics

  • State:closed
  • Created 5 months ago
  • Reactions:2
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
tomjebocommented, Apr 6, 2023

@twsouthwick @mikeebowen This is the same situation with AddImagePart. I’ll also refactor AddImagePart to use a single extension method while I’m in here.

0reactions
tomjebocommented, Jun 27, 2023

@tomjebo can this be closed?

Yup!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can I add extension methods to an existing static class?
No. Extension methods require an instance variable (value) for an object. You can however, write a static wrapper around the ...
Read more >
Extension Methods - C# Programming Guide
Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the ...
Read more >
Extend Existing Objects Functionalities with C# Extension ...
With C# extension methods you can extend objects that you might not even have the source code from! In this video we will...
Read more >
Extend your C# objects with custom methods seamlessly!
Extension methods enable you to “add” methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type....
Read more >
Chapter 10. Extension methods - C# in Depth
In this chapter we'll first look at how to use extension methods and how to write them. We'll then examine a few of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found