1

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?

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.

Tom
  • 15,781
  • 14
  • 69
  • 111
  • Why must you use an extension method? Have the extensions class act as a function provider to do on IServiceLocator. – SimpleVar May 02 '12 at 08:53

4 Answers4

6

Just remove this keyword from extension methods.

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. Btw suffix Extensions you can remove too. E.g. use Helper to not confuse people.

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
2
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/

empi
  • 15,755
  • 8
  • 62
  • 78
1

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.

But this one, probabbly, will involve more work then other solutions oferred here, by the way will produce more consistent result.

Tigran
  • 61,654
  • 8
  • 86
  • 123
1

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.

Community
  • 1
  • 1
Styxxy
  • 7,462
  • 3
  • 40
  • 45