在C#中制作通用方法

时间:2012-02-21 08:18:45

标签: generics c#-4.0

我有一个泛型类,其方法在

下定义
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;
        }

实际上除了ResponseType ...

之外,两个Execute方法之间没有区别

我在

下调用方法
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);
        }

但是我想让Execute方法成为通用的,这样对于每个响应类型我都不应该写一个新的方法..是否可能,如果有的话请帮助我这样做..

由于

2 个答案:

答案 0 :(得分:1)

声明并定义如下的执行方法

public T Execute<T>(Func<X, T> clientCall)
{
    T 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;
}

并像这样调用此方法

var result = Execute<PartnerListingResponse >(client => client.GetRenewalListingRecords(listingRequest));

答案 1 :(得分:0)

我建议创建一个像ListingResponse这样的基类,从中派生出两个子类RenewalListingResponsePartnerListingResponse

然后你只有一个方法适用于所有类型的ListingResponse:

public ListingResponse Execute(Func<X, ListingResponse> clientCall)
    {
        ListingResponse response = null;

        using (dynamic client = new X())
        {
            try
            {
                // invoke client method passed as method parameter
                response = clientCall(client);

            }
            ...
         }
        return response;
     }