3

I want to build a Windows Store class library using source code from a regular .NET Framework class library. Ideally, I do not want to modify the original source code files.

In some of the source code files from the .NET Framework library, static members are used from a class that is defined in both the regular .NET Framework API and the .NET for Windows Store apps API, but where only a subset of the .NET Framework members are available for Windows Store.

One specific example is System.IO.Path, where the GetFullPath method is not available for Windows Store apps.

It is fairly straightforward to incorporate a replacement for this method in my Windows Store class library and have the original source code invoke this method instead. My question is, is there any way I can do this without modifying the original source code file?

So far, I have not been able to figure out a satisfactory solution to this problem, but I have solved it for my Windows Store class library by implementing e.g. the Path.GetFullPath(string) method in another namespace:

namespace WindowsStoreLib.System.IO {
    public static class Path {
        public static string GetFullPath(string path) { ... }
    }
}

and then adding a preprocessor directive in the original files:

#if NETFX_CORE
using Path = WindowsStoreLib.System.IO.Path;
#endif

Is there an alternative solution to this issue that does not require modification of the original source code files?

Anders Gustafsson
  • 15,837
  • 8
  • 56
  • 114
  • Make two versions of your library, one that reimplements the needed stuff, another that just calls the native version, and include a different one depending on which version of your app it is. – millimoose Mar 22 '13 at 12:44
  • 2
    The short answer is: No, you can't add static methods to existing classes. – Daniel Hilgarth Mar 22 '13 at 12:46
  • @millimoose I want to be able to reuse the original source code from the .NET Framework based library without modification, and in those files for example `Path.GetFullPath()` is used. – Anders Gustafsson Mar 22 '13 at 12:46
  • The adapter-pattern could be helpful for you. – Tomtom Mar 22 '13 at 12:48

3 Answers3

5

No, you cannot, simply.

When I'm doing cross-platform stuff I tend to write a utility class that has different implementations (via #if) for different platforms - then my core code just calls the utility class.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Thanks, Marc. Yes, I normally use the "utility class" approach myself, but I was kind of hoping that there was something I had overlooked :-) Apparently not, but at least it is good to get an independent confirmation. – Anders Gustafsson Mar 22 '13 at 12:50
0

I've been doing something like this lately with Entity Framework classes since I needed to add a specific output of 2 fields as 1 and it wiped it out from the designer.cs on every update. However they were not static classes or static methods, but should work with same.

Create a new class file with the name of the class you want to extend and use the partial qualifier.

namespace WindowsStoreLib.System.IO {
    public partial static class Path {

       public static string GetFullPath(string path) { ... }
    }
}
lostcny
  • 31
  • 2
  • Thanks, @lostcny. The problem is that I will **not** be able to reference that method (or even build the library), even if it is within a partial class, unless I modify the original source code files. And modification was what I wanted to avoid. – Anders Gustafsson Mar 22 '13 at 12:54
0

As Marc said, the preprocessor directive seems to be the only solution.

But when I read "static class" and "existing class", the first thing coming to my mind is "extension method". What would happen if you created an extension method for Path in the same namespace where your code is?

namespace MyNamespace.WhereMyCodeIs
{
    using System.IO;

    public static class ExtensionMethods
    {
        public static string GetFullPath(this Path pathObject, string path) 
        {
            // Implementation
        }
    }
}

I am really not sure if this would work out but maybe there is a work around we could find around this.

Ucodia
  • 7,410
  • 11
  • 47
  • 81
  • 3
    Extension methods only work on instances. You can't use them to make it look like there are new static methods on an existing class (though there are plenty of times I've wished you could). – Joe White Mar 22 '13 at 13:13
  • Oh yeah totally true. I also wished that on many occasions. Forget it! – Ucodia Mar 22 '13 at 13:14
  • 1
    Thanks, @Ucodia, but as Joe points out (thanks, Joe) you need an instance to use extension methods. I have considered it, but also not been able to come up with a viable solution using extension methods. – Anders Gustafsson Mar 22 '13 at 13:28