C# C语言中的聚合接口#
我有一门课:C# C语言中的聚合接口#,c#,.net,interface,C#,.net,Interface,我有一门课: class UrlManagementServiceClient : System.ServiceModel.ClientBase<IUrlManagementService>, IUrlManagementService 但我无法将UrlManagementServiceClient对象强制转换为IUrlManagementProxy。有没有办法做到这一点?我希望最终得到一个可以访问所有三个接口上所有方法的对象。使UrlManagementServiceClient
class UrlManagementServiceClient : System.ServiceModel.ClientBase<IUrlManagementService>, IUrlManagementService
但我无法将
UrlManagementServiceClient
对象强制转换为IUrlManagementProxy
。有没有办法做到这一点?我希望最终得到一个可以访问所有三个接口上所有方法的对象。使UrlManagementServiceClient
实现IUrlManagementProxy
而不是IUrlManagementService
class UrlManagementServiceClient : System.ServiceModel.ClientBase<IUrlManagementProxy>, IUrlManagementProxy
类UrlManagementServiceClient:System.ServiceModel.ClientBase,IUrlManagementProxy
您需要在UrlManagementServiceClient
上实现IUrlManagementProxy
。没有其他方法-它是一种独立的类型。您只能强制转换到继承自的接口,才能强制转换到需要实现该接口的IUrlManagementProxy
class UrlManagementServiceClient :
System.ServiceModel.ClientBase<IUrlManagementService>, IUrlManagementProxy
在另一个代码文件中,您的类也将实现完整的
IUrlManagementProxy
接口,然后您可以将其强制转换为IUrlManagementProxy
,显然,您无法将对象强制转换为它未实现的接口
但是,您可以做的是(如果从UrlManagementServiceClient
实例中为每个接口实现所有方法是有意义的),将UrlManagementServiceClient
包装到实现所需接口的对象中
这称为(而不是代理)。代理通常“显示”为基础对象,而在本例中,您添加的是客户端所没有的功能
换句话说,您需要一个新类:
public class UrlManagementClientProxy : IUrlManagementProxy
{
// we need a reference to the underlying client
private readonly UrlManagementServiceClient _client;
// underlying client is passed to the proxy in constructor
public UrlManagementClientProxy(UrlManagementServiceClient client)
{
_client = client;
}
#region IUrlManagementProxy methods
// you implementation goes here. if the underlying client
// already implements a certain method, then you just need
// to pass the call
// for example, client already implements methods
// from the IUrlManagementService interface, so use them
public string GetUrl() // made up
{
return _client.GetUrl();
}
#endregion
}
这允许您重用客户端的实现,并在其上添加其他功能。为了解决这个问题,我只是扩展了该类并在其上声明了聚合接口:
public class UrlManagementProxy : UrlManagementServiceClient, IUrlManagementProxy
{
public UrlManagementProxy(Binding binding, EndpointAddress remoteAddress)
: base(binding, remoteAddress)
{
}
}
然后我使用UrlManagementProxy
而不是UrlManagementServiceClient
我只需要通过我需要的构造函数。其余的都是自动处理的。同样,通过这种方式,我不需要修改
UrlManagementServiceClient
,这样我就可以重新生成它,并且一切都可以正常工作。UrlManagementServiceClient是一个自动生成的WCF客户端。我希望避免对文件进行手动编辑,以防需要重新生成。@RandomEngy,您可以在单独的文件中使用自己的接口添加部分类定义,请参见编辑我的答案。尝试了此操作,但需要在ICommunicationObject和IUrlManagementService上实现大量内容,结果变得越来越混乱。我采用了另一种方法。
public class UrlManagementClientProxy : IUrlManagementProxy
{
// we need a reference to the underlying client
private readonly UrlManagementServiceClient _client;
// underlying client is passed to the proxy in constructor
public UrlManagementClientProxy(UrlManagementServiceClient client)
{
_client = client;
}
#region IUrlManagementProxy methods
// you implementation goes here. if the underlying client
// already implements a certain method, then you just need
// to pass the call
// for example, client already implements methods
// from the IUrlManagementService interface, so use them
public string GetUrl() // made up
{
return _client.GetUrl();
}
#endregion
}
public class UrlManagementProxy : UrlManagementServiceClient, IUrlManagementProxy
{
public UrlManagementProxy(Binding binding, EndpointAddress remoteAddress)
: base(binding, remoteAddress)
{
}
}