Can I “add” static methods to existing class in the .NET API?

会有一股神秘感。 提交于 2019-12-12 13:21:09

问题


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?


回答1:


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.




回答2:


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) { ... }
    }
}



回答3:


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.



来源:https://stackoverflow.com/questions/15570629/can-i-add-static-methods-to-existing-class-in-the-net-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!