views:

50

answers:

3

I'm trying to 'genericize' some code we have spattered around our system.

I want to:

  1. return a generic type,
  2. pass in some kind of delegate containing the method to be called.

I'm pretty new to generics so any help appreciated.

Below is where my finger in the air is(!)

public static T ReturnSingleObject<T>(Func<string, int, T> dynamicSignature)
    {
        T returnValue;
        ServiceReference wCFService;
        try
        {
            wCFService = new BusinessServiceClient();

            returnValue = dynamicSignature();

            //returnValue = wCFService.AMETHOD(PARAM1, PARAM2);
            return returnValue;
        }
        catch (Exception)
        {
            if (wCFService != null) wCFService.Abort();
            throw;
        }
        finally
        {
            if (wCFService != null) wCFService.Close();
        }
    }
A: 

You should check out this link it deals with declaring a generic delegate: http://msdn.microsoft.com/en-us/library/sx2bwtw7.aspx

pstrjds
+2  A: 

It looks like you're missing a couple parameters for your dynamicSignature function, so you'll need to add those. Also, you might move your return statement to the bottom, out of the try block, and initialize your return value to the default value:

T returnValue = default(T);
...
try
{
   ...
   returnValue = dynamicSignature(somestring, someint);
   ...
}
...

return returnValue;
Mark Synowiec
A: 

you need to understand Generics first before applying it. Try to read it on MSDN

saurabh