Skip to main content
deleted 4 characters in body
Source Link
Thorsten Dittmar
  • 56.9k
  • 10
  • 95
  • 145

If they have no common base class or interface, I can think of two ways of achieving what you want:

Pass an object
Make your method take an object parameter and use the is operator to find out what you got, maybe like this:

public void CallMethod(object service) { if (service is Localservice) (service as (Localservice)service).CallMethod(); else (service as (Amazonservice)service).CallMethod(); } 

Create a proxy class
You might also encapsulate every call in a proxy class that has both service references and decides which one to use. You can then rely on the proxy class to do the right thing:

public class ProxyClass { private Localservice _localService; private Amazonservice _amazonService; public ProxyClass(Localservice instance1, Amazonservice instance2) { _localService = instance1; _amazonService = instance2; } public void CallMethod() { if (shouldUseLocalService) _localService.CallMethod(); else _amazonService.CallMethod(); } } 

You can then make your actual method like this:

public void CallMethod(ProxyClass proxy) { proxy.CallMethod(); } 

If they have no common base class or interface, I can think of two ways of achieving what you want:

Pass an object
Make your method take an object parameter and use the is operator to find out what you got, maybe like this:

public void CallMethod(object service) { if (service is Localservice) (service as Localservice).CallMethod(); else (service as Amazonservice).CallMethod(); } 

Create a proxy class
You might also encapsulate every call in a proxy class that has both service references and decides which one to use. You can then rely on the proxy class to do the right thing:

public class ProxyClass { private Localservice _localService; private Amazonservice _amazonService; public ProxyClass(Localservice instance1, Amazonservice instance2) { _localService = instance1; _amazonService = instance2; } public void CallMethod() { if (shouldUseLocalService) _localService.CallMethod(); else _amazonService.CallMethod(); } } 

You can then make your actual method like this:

public void CallMethod(ProxyClass proxy) { proxy.CallMethod(); } 

If they have no common base class or interface, I can think of two ways of achieving what you want:

Pass an object
Make your method take an object parameter and use the is operator to find out what you got, maybe like this:

public void CallMethod(object service) { if (service is Localservice) ((Localservice)service).CallMethod(); else ((Amazonservice)service).CallMethod(); } 

Create a proxy class
You might also encapsulate every call in a proxy class that has both service references and decides which one to use. You can then rely on the proxy class to do the right thing:

public class ProxyClass { private Localservice _localService; private Amazonservice _amazonService; public ProxyClass(Localservice instance1, Amazonservice instance2) { _localService = instance1; _amazonService = instance2; } public void CallMethod() { if (shouldUseLocalService) _localService.CallMethod(); else _amazonService.CallMethod(); } } 

You can then make your actual method like this:

public void CallMethod(ProxyClass proxy) { proxy.CallMethod(); } 
Source Link
Thorsten Dittmar
  • 56.9k
  • 10
  • 95
  • 145

If they have no common base class or interface, I can think of two ways of achieving what you want:

Pass an object
Make your method take an object parameter and use the is operator to find out what you got, maybe like this:

public void CallMethod(object service) { if (service is Localservice) (service as Localservice).CallMethod(); else (service as Amazonservice).CallMethod(); } 

Create a proxy class
You might also encapsulate every call in a proxy class that has both service references and decides which one to use. You can then rely on the proxy class to do the right thing:

public class ProxyClass { private Localservice _localService; private Amazonservice _amazonService; public ProxyClass(Localservice instance1, Amazonservice instance2) { _localService = instance1; _amazonService = instance2; } public void CallMethod() { if (shouldUseLocalService) _localService.CallMethod(); else _amazonService.CallMethod(); } } 

You can then make your actual method like this:

public void CallMethod(ProxyClass proxy) { proxy.CallMethod(); }