2010-11-23 72 views
0

我有多个wcfClients(从网上引用设计),它们都实现了自己的接口,而接口又都继承了另一个接口。投射WCFClient(或使用泛型集合?)

我想打电话给在所有的网络服务都继承了接口中的方法,所以不是这样......

case "DVSSync": 
         DVSSync.WcfDVSSyncClient dvsSyncClient = new DVSSync.WcfDVSSyncClient("BasicHttpBinding_IWcfDVSSync1"); 

         dataRow["URI"] = dvsSyncClient.Endpoint.Address.ToString(); 
         dataRow["ServiceUptime"] = dvsSyncClient.ServiceUptime(); 
         dataRow["Version"] = dvsSyncClient.Version(); 

         dvsSyncClient.Close(); 
         break; 

        case "DataInserter": 
         DataInserter.WcfDataInserterClient dataInserterClient = new DataInserter.WcfDataInserterClient("BasicHttpBinding_IWcfDataInserter1"); 

         dataRow["URI"] = dataInserterClient.Endpoint.Address.ToString(); 
         dataRow["ServiceUptime"] = dataInserterClient.ServiceUptime(); 
         dataRow["Version"] = dataInserterClient.Version(); 

         dataInserterClient.Close(); 
         break; 

我想要做同样的事情到

switch (service) 
     { 
     case "DVSSync": 
            DVSSync.WcfDVSSyncClient dvsSyncClient = new DVSSync.WcfDVSSyncClient("BasicHttpBinding_IWcfDVSSync1"); 

    GenericClient wcfClient = (GenericClient)dvsSyncClient; 


            break; 

           case "DataInserter": 
            DataInserter.WcfDataInserterClient dataInserterClient = new DataInserter.WcfDataInserterClient("BasicHttpBinding_IWcfDataInserter1"); 

    GenericClient wcfClient = (GenericClient)dataInserterClient ; 

            break; 

     } 

            dataRow["URI"] = wcfClient.Endpoint.Address.ToString(); 
            dataRow["ServiceUptime"] = wcfClient.ServiceUptime(); 
            dataRow["Version"] = wcfClient.Version(); 

            wcfClient.Close(); 

谢谢!

回答

1

什么是这样的:

void Foo() 
{ 
    GenericClient client = CreateClient(service); 
    //do stuff with generic client 
} 

GenericClient CreateClient(string service) 
{ 
    switch(service) 
    { 
    case "DVSSync": 
     return new WcfDVSSyncClient() 
    //etc 
    } 
}