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:
- Created 5 months ago
- Reactions:2
- Comments:5 (1 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@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.
Yup!