我有方法的泛型类定义为下使在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有可能,如果是请帮我这样做..
感谢