开发者

get return value with DynamicInvoke or generic delegate

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!


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());
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜