2010-11-23 83 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 
    } 
}