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 6957647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:01:55+00:00 2026-05-27T15:01:55+00:00

I would like to provide encapsulation of actions/methods generically. I think this should be

  • 0

I would like to provide encapsulation of actions/methods generically. I think this should be possible in C# but I’m not able to produce it so it compiles…

The following briefly demonstrates what I want. Is this possible somehow, perhaps by generalizing the class?

Required is:

  • I want to execute the function/action (see method type) and ‘do something’ when an error occurs
  • I want to return the value of the function if the method is a function (otherwise return void if possible)
  • I want to ‘do something’ if the return type of the function is a boolean and the value is false.

    public class Encapsulator {
        private Action _action;
        private Func<T> _function;
        private MethodType _type; //Action || Function
    
        public Encapsulator(Action action) {
            this._action = action;
            this._type = MethodType.Action;
        }
        public Encapsulator(Func<T> func) { //This is not accepted
            this._function = func;
            this._type = MethodType.Function;        
        }
    
        public void Execute() {
            try {
                this._action();
            }
            catch(Exception ex) {
                //do something
                throw;
            }
        }
    
        public T Execute<T>() {
            try {
                var r = this._function();
    
                if(typeof(r) == bool) {
                    if(!r)
                      //do something
                }
    
                return r;
    
            } catch(Exception ex) {
                //do something
                throw;
            }
        }
    }
    
  • 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-27T15:01:55+00:00Added an answer on May 27, 2026 at 3:01 pm

    New Approach: The action Encapsulator will now return a dummy result (always true).

    public class Encapsulator
    {
        public static Encapsulator<bool> CreateEncapsulator(Action action)
        {
            return new Encapsulator<bool>(() =>
                {
                    action();
                    return true;
                });
        }
    
        public static Encapsulator<T> CreateEncapsulator<T>(Func<T> func)
        {
            return new Encapsulator<T>(func);
        }
    }
    
    public class Encapsulator<T>
    {
        private Func<T> _function;
    
        internal Encapsulator(Func<T> func)
        {
            this._function = func;
        }
    
        public T Execute()
        {
            try
            {
                object res = this._function();
                Nullable<bool> bres = res as Nullable<bool>;
    
                if (bres.HasValue)
                {
                    if (!bres.Value)
                        Console.WriteLine("NOT TRUE!");
                    //do something
                }
    
                return (T)res;
    
            }
            catch (Exception ex)
            {
                //do something
                throw;
            }
        }
    }
    

    Calling the code:

    var withDummyReturn = Encapsulator.CreateEncapsulator(() => Console.WriteLine("Great"));
    withDummyReturn.Execute();
    var withReturn = Encapsulator.CreateEncapsulator<bool>(() => true);
    bool res = withReturn.Execute();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to provide some custom Assert methods in my MSTest Unit Testing
Currently, I would like provide this as an option to the user when storing
I would like provide and animation effect but i don't how to do. Even
I would like to provide two different versions of my iPhone app on the
I would like to provide the raw text referring to an environment variable to
I would like to provide the same content inside 2 different base files. So,
I would like to provide a PHP hanging request that will stay open until
I have developed about 300 Applications which I would like to provide with multi-language
I'm building a plugin assembly and I would like to provide an accompanying config
I am writing and publishing my apps on android and would like to provide

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.