1

i am trying to create a class called SurroundJob that accepts Func

but executing the incoming method is not its purpose, the task of SurroundJob is to do certain predefined things before and after calling the incoming method

however, i am having trouble getting the return value of the incoming method, so that i could pass it to the calling class :(

appended first the calling class, then the desired, but currently non-functional, 'surrounder' class and finally the exception due to the inappropriate cast attempt (TResult)method.DynamicInvoke(param)

The Calling Class

class ACoreJob
{

    public void DoMyJob()
    {
        SurroundJob.CoreJobs<Boolean, string> coreJob = DoCoreJob;
        Boolean success = false;
        SurroundJob.Embed<Boolean, string>(ref success, "facebook.com", coreJob);
        if (success) Trace.WriteLine("Wicked without exceptions");
    }

    Boolean DoCoreJob(string Target)
    {
        Boolean isHappy = false;
        Process.Start(@"http://" + Target);
        isHappy = true;
        return isHappy;
    }

}

The Class in Focus

class SurroundJob
{

    public delegate TResult CoreJobs<TResult, T>(T param);

    public static void Embed<TResult,T>(ref TResult result,T param, Delegate method)
    {
        if (method != null)
        {
             MethodInfo methodInfo = method.Method;
             result = default(TResult);
             try
             {
                 Log(methodInfo.Name + " Start");
                 result = (TResult)method.DynamicInvoke(param);                     
             }
             catch (Exception e)
             {
                 Troubleshoot(methodInfo.Name, e);
             }
        }
    }

The Exception

At line: result = (TResult)method.DynamicInvoke(param);

DoCoreJob Problem: Unable to cast object of type 'ACoreJob' to type 'Boolean'.

i am new to this world and dont really know how to interact with DynamicInvoke in order to get the return value?

or is there another way to achieve my aim?

thank you sincerely!

Cel
  • 6,467
  • 8
  • 75
  • 110
  • 1
    Why have you not only decided to create your own delegate type when `Func` is already in the framework, but also decided to change the order of the operands? – Jon Skeet Aug 01 '11 at 15:14
  • 1
    The exception you've logged talks about two types which you haven't mentioned *anywhere* else in the question. It's also not clear why you're using `Delegate` when you clearly expect it to take a T and return a TResult - why not just change the method parameter to `CoreJobs`? – Jon Skeet Aug 01 '11 at 15:16
  • I've just copy and pasted your code and I didn't get any exceptions so you may need to look outside of what you've posted for the problem. – DoctorMick Aug 01 '11 at 15:22

1 Answers1

1

Here's a simple example using Func<T, TResult>:

void Main()
{
    bool success = false;
    SurroundJob.Embed(ref success, "facebook.com", DoCoreJob);
}

Boolean DoCoreJob(string Target)
{
    Boolean isHappy = false;
    Console.WriteLine(@"http://" + Target);
    isHappy = true;
    return isHappy;
}

class SurroundJob
{
    public static void Embed<T, TResult>(ref TResult Result, T param,  Func<T, TResult> method)
    {
        if(method != null)
        {
            try
            {
                Log(param.ToString());
                Result = method(param);
            }
            catch(Exception e)
            {
                Troubleshoot(param.ToString(), e);
            }
        }
    }

    public static void Log(string s)
    {
        Console.WriteLine("Log: " + s);
    }

    public static void Troubleshoot(string s, Exception e)
    {
        Console.WriteLine("Troubleshoot: " + s);
        Console.WriteLine(e.ToString());
    }
}
George Duckett
  • 31,770
  • 9
  • 95
  • 162
  • it's the unstraight ways of trying to find a solution that brought me to DynamicInvoke when having Func as the argument for Embed does miracles! thank you! and i had no idea i could skip the coreJob and specifications in DoMyJob() ! – Cel Aug 01 '11 at 15:47
  • by the way, does this kind of repetitive Fixed PreTasks > Variable CoreTask > Fixed PostTasks issue relate to any labelled design patterns? – Cel Aug 01 '11 at 15:50
  • I'm not 100% sure, so this is probably best left to another question. – George Duckett Aug 01 '11 at 15:55
  • turns out this attempt is a rudimentary form of [Aspect Oriented Programming](http://c2.com/cgi/wiki?AspectOrientedProgramming), and can be implemented much more neatly with [PostSharp](http://stackoverflow.com/questions/352527/adding-an-onexception-attribute-using-postsharp) or [Unity](http://www.progware.org/Blog/post/Interception-and-Interceptors-in-C-%28Aspect-oriented-programming%29.aspx), for instance! – Cel Aug 28 '11 at 18:25