-1

I am making a program that processes thousands of files. For each file I create a FileInfo instance, but I am missing some methods and properties that I need.

I wanted to make my own custom FileInfo class by inherting from FileInfo, but that class is sealed so I can't.

I considered making extension methods for FileInfo, but that seems ugly and it requires me to run the same code multiple times while processing. So instead I came up with a custom class that 'wraps' the FileInfo class.

Here's a part of it:

class MyFileInfo
{
    FileInfo _fileInfo;

    // wrapper of existing property
    public string Name { get { return _fileInfo.Name; } }

    // custom property
    public string NameWithoutExtension { get; private set; }

    // custom property
    public string Increment { get; private set; }

    public MyFileInfo(string filePath)
    {
        _fileInfo = new FileInfo(filePath);

        NameWithoutExtension = GetNameWithoutExtension();
        Increment = GetIncrement();
    }

    private string GetNameWithoutExtension()
    {
        return _fileInfo.Name.Replace(_fileInfo.Extension, string.Empty);
    }

    private string GetIncrement()
    {
        return Regex.Match(NameWithoutExtension, @" #?\d{1,4}$").Value;
    }
}

Now my question is: is this the best way to do this? How else could one work around not being able to inherit a sealed class?

Mytzenka
  • 205
  • 1
  • 3
  • 16
  • Can you explain the problem you had with extension methods? – 15ee8f99-57ff-4f92-890c-b56153 Oct 02 '19 at 17:06
  • With extension methods I would call them multiple times, thus running the same code multiple times. Whereas if I have a class I can do all I need to do and set the custom properties only once in the constructor, which seems more efficient. – Mytzenka Oct 03 '19 at 05:44
  • Makes sense, if it's happening enough that there's noticeable overhead. Incidentally, there's a [`Path.GetFilenameWithoutExtension()` method](https://learn.microsoft.com/en-us/dotnet/api/system.io.path.getfilenamewithoutextension?view=netframework-4.8), which will be safer and likely faster than what you've got there. In answer to (what I think is) the meat of your question: What you're doing looks to me like a sensible way to trade memory for cycles, but when you're optimizing, profile everything to be sure you're attacking the real problem and doing so effectively. – 15ee8f99-57ff-4f92-890c-b56153 Oct 03 '19 at 12:29

1 Answers1

2

You have done it almost right, The solution to your problem is exactly the use of decorator pattern.

The decorator pattern is a design pattern that allows the behavior to be added to an individual object, either statically or dynamically, without affecting the behavior of other objects from the same class.

Check these posts for more details:

1- UnderstandingplusandplusImplementingplusDecoratorp

2- benefiting-with-the-decorator-pattern

AminSojoudi
  • 1,904
  • 2
  • 18
  • 42
  • Thank you for your help! I think I understand the decorator pattern now but I'm not sure yet why I should use this over the solution I put above. With the decorator I would make one or more new classes with the functionality I want to add and then chain them in the instantiating of FileInfo right? So for example: `FileInfo fileInfo = new FileInfo().ExtensionClass()` – Mytzenka Oct 04 '19 at 03:20