Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aggregating interfaces in C#

Tags:

c#

.net

interface

I've got this class:

class UrlManagementServiceClient : System.ServiceModel.ClientBase<IUrlManagementService>, IUrlManagementService

ClientBase implements IDisposable and ICommunicationObject. I've also got this interface:

interface IUrlManagementProxy : IUrlManagementService, ICommunicationObject, IDisposable

But I can't cast UrlManagementServiceClient objects to IUrlManagementProxy. Is there some way to accomplish this? I want to end up with an object that can access all the methods on all three interfaces.

like image 305
RandomEngy Avatar asked May 17 '11 19:05

RandomEngy


1 Answers

You can only cast to interfaces that you inherit from, to be able to cast to IUrlManagementProxy you need to implement that interface.

class UrlManagementServiceClient :
   System.ServiceModel.ClientBase<IUrlManagementService>, IUrlManagementProxy

You can then cast UrlManagementServiceClient to either UrlManagementProxy, IUrlManagementService, ICommunicationObject or IDisposable.

Edit
The WCF-generated classes are partial, that means that you can extend the class definition in another file. Put

public partial class UrlManagementServiceClient : IUrlManagementProxy {}

in another code file and your class will implement your full IUrlManagementProxy interface too and you can then cast it to IUrlManagementProxy.

like image 83
Albin Sunnanbo Avatar answered Sep 28 '22 09:09

Albin Sunnanbo