I&QUOT能;新增]静态方法对现有类.NET API吗? [英] Can I "add" static methods to existing class in the .NET API?

查看:179
本文介绍了I&QUOT能;新增]静态方法对现有类.NET API吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想建立一个的 Windows应用商店的类库的使用源代码从一个普通的 .NET Framework类库的。理想情况下,我不希望修改原始源代码文件。

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.

在一些源代码文件的从 .NET Framework库的,静态部件从设置在两个常规定义的类中使用的.NET Framework 的API和的 .NET的Windows Store应用程序的API,但其中只有一个子集的的.NET Framework 的成员可供 Windows应用商店

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.

一个具体的例子是的 System.IO.Path ,其中的 GetFullPath方法不适用于的 Windows应用商店的应用

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

这是相当简单的合并替换在我的 Windows应用商店的类库,该方法的,并有原始源代码调用此方法来代替。我的问题是,是否有任何办法可以做到这一点无需修改原始源代码文件?

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?

到目前为止,我还没有能够找出一个满意的解决了这个问题,但我已经通过实施如解决了我的 Windows应用商店的类库的在另一个命名空间中的 Path.GetFullPath(串)方法:

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?

推荐答案

没有,你不行,干脆。

当我在做跨平台的东西,我倾向于写有不同的实现(通过#如果)的实用程序类为不同的平台 - 然后我的核心代码只是调用工具类

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.

这篇关于I&QUOT能;新增]静态方法对现有类.NET API吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆