简体   繁体   English

.NET 2.0等效于C#扩展方法

[英].NET 2.0 equivalent of C# extension method

How do I do change this piece of code to .NET 2.0 compatible by replacing the extension method to .NET 2.0 of equivalent? 如何通过将扩展方法替换为等效的.NET 2.0来将这段代码更改为.NET 2.0兼容?

public interface IMessagingService {
    void sendMessage(object msg);
}
public interface IServiceLocator {
    object GetService(Type serviceType);
}
public static class ServiceLocatorExtenstions {
    //.NET 3.5 or later extension method, .NET 2 or earlier doesn't like it
    public static T GetService<T>(this IServiceLocator loc) {
        return (T)loc.GetService(typeof(T));
    }
}
public class MessagingServiceX : IMessagingService {
    public void sendMessage(object msg) {
        // do something
    }
}
public class ServiceLocatorY : IServiceLocator {
    public object GetService(Type serviceType) {
        return null; // do something
    }
}
public class NotificationSystem {
    private IMessagingService svc;
    public NotificationSystem(IServiceLocator loc) {
        svc = loc.GetService<IMessagingService>();
    }
}
public class MainClass {
    public void DoWork() {
        var sly = new ServiceLocatorY();
        var ntf = new NotificationSystem(sly);
    }
}

Thank you very much. 非常感谢你。

Just remove this keyword from extension methods. 只需从扩展方法中删除this关键字即可。

public static class ServiceLocatorExtensions
{    
    public static T GetService<T>(IServiceLocator loc) {
        return (T)loc.GetService(typeof(T));
    }
}

And call it as any other static method by passing instance of object which you are 'extending': 并通过传递您要“扩展”的对象实例,将其作为其他任何静态方法调用:

IServiceLocator loc = GetServiceLocator();
Foo foo = ServiceLocatorExtensions.GetService<Foo>(loc);

Actually this is what .Net 3.5 compiler does behind the scene. 实际上,这就是.Net 3.5编译器在后台执行的操作。 Btw suffix Extensions you can remove too. Btw后缀您也可以删除Extensions Eg use Helper to not confuse people. 例如,使用Helper不会使人们感到困惑。

svc = loc.GetService<IMessagingService>();

equals 等于

svc = ServiceLocatorExtenstions.GetService<IMessagingService>(loc);

However, you don't have to remove extension methods and still target .NET 2.0 - check this post (more on google): http://kohari.org/2008/04/04/extension-methods-in-net-20/ 但是,您不必删除扩展方法,也仍然可以定位.NET 2.0-检查此信息(在Google上有更多信息): http : //kohari.org/2008/04/04/extension-methods-in-net-20 /

If you don't want to use extension methods and avoid ambiguity in your code, the clanest solution is to move all ServiceLocatorExtenstions methods inside your IServiceLocator interface definition and remove ServiceLocatorExtenstions class. 如果您不想使用扩展方法并避免代码中的歧义,最直接的解决方案是将所有ServiceLocatorExtenstions方法移至IServiceLocator接口定义内,并删除ServiceLocatorExtenstions类。

But this one, probabbly, will involve more work then other solutions oferred here, by the way will produce more consistent result. 但这可能会比这里提供的其他解决方案涉及更多的工作,顺便会产生更一致的结果。

Why not put the generic method in your interface (as well)? 为什么不将通用方法也放入您的界面? Since your extension method only makes the call easier, isn't it better to make it easier in the first place? 由于您的扩展方法只会使调用更容易,因此首先使其变得更容易吗?

There are ways of having extension methods in .NET 2.0: see here or here . .NET 2.0中有多种扩展方法:请参见此处此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM