我有方法的泛型類定義爲下使在C#泛型方法
public abstract class BaseService<X> where X : new()
{
public PartnerListingResponse Execute(Func<X, PartnerListingResponse> clientCall)
{
PartnerListingResponse response = null;
using (dynamic client = new X())
{
try
{
// invoke client method passed as method parameter
response = clientCall(client);
}
catch (TimeoutException ex)
{
Console.WriteLine("The service operation timed out. " + ex.Message);
client.Abort();
}
catch (CommunicationException ex)
{
Console.WriteLine("There was a communication problem. " + ex.Message + ex.StackTrace);
client.Abort();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
client.Abort();
}
}
return response;
}
public RenewalListingResponse Execute(Func<X, RenewalListingResponse> clientCall)
{
RenewalListingResponse response = null;
using (dynamic client = new X())
{
try
{
// invoke client method passed as method parameter
response = clientCall(client);
}
catch (TimeoutException ex)
{
Console.WriteLine("The service operation timed out. " + ex.Message);
client.Abort();
}
catch (CommunicationException ex)
{
Console.WriteLine("There was a communication problem. " + ex.Message + ex.StackTrace);
client.Abort();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
client.Abort();
}
}
return response;
}
其實,有兩個沒有什麼區別Execute方法除了responseTyp的...
我調用方法爲
public Tuple<int, string, List<PartnerListing>> GetEarningListingRecords(string uRL)
{
//Create the request
PartnerListingRequest listingRequest = new PartnerListingRequest();
listingRequest.URL = uRL;
//Send the request and get the service response
var result = Execute(client => client.GetEarningListingRecords(listingRequest));
//Send the response back to client
return Tuple.Create(result.ResultCode, result.ResultMessage, result.PartnerListings);
}
public Tuple<int, string, List<RenewalListing>> GetRenewalListingRecords(int PartnerId)
{
//Create the request
RenewalListingRequest listingRequest = new RenewalListingRequest();
listingRequest.PartnerId = PartnerId;
//Send the request and get the service response
var result = Execute(client => client.GetRenewalListingRecords(listingRequest));
//Send the response back to client
return Tuple.Create(result.ResultCode, result.ResultMessage, result.RenewalListings);
}
下,但我想使執行方法作爲通用的,所以,對於每一個響應類型我不應該寫一個新的method..is有可能,如果是請幫我這樣做..
感謝