Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6215589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:05:07+00:00 2026-05-24T07:05:07+00:00

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

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T07:05:08+00:00Added an answer on May 24, 2026 at 7:05 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a point class which defines a property called coordinate. However,
I am trying to create a class that implements the IUnknown interface. I have
I'm trying to create a class that can instantiate arrays at runtime by giving
I'm trying to create a generic class in PHP that will provide a way
I'm trying to create my own class template for Visual Studio called Public Class.
I'm trying to create a class TestClass that's divided over several files. I have
I'm trying to create a class with a built-in condition for one of its
I'm trying to create a C++ dynamic library that won't have a class. I'd
I'm trying to create a class called HighscoresController which subclasses NSObject. When I call
I'm trying to create a class in java that pool objects. The class starts

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.