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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:51:26+00:00 2026-06-04T01:51:26+00:00

I am creating a program where the user creates custom commands and execute them

  • 0

I am creating a program where the user creates custom commands and execute them when needed. as a result I have a class similar to:

public class Command
{
    Action c { get; set; }

    // Overloaded Constructors------------------------------------

      // changes the volume                
      public Command(int volumeChange)
      {
        c = ()=>
            SomeClass.ChangeMasterVolume(volumeChange);
      }

      // Animate something
      public Command(int x, int y)
      {
        c = ()=>
            SomeClass.MoveMouse(x,y);
      }

      // etc.. there are more contructors....

   //---------------------------------------------------------


    public void ExecuteCommand()
    {
        c();
    }
}

When the user closes the application I will like to save those commands somewhere on disk. There are about 200 different commands and it will be nice if I could serialize an instance from that class. Since it contains an Action it is not possible to serialize it.

It will be nice if I don’t have to create a huge switch statement in order to determine what command to execute. What is the best way of dealing with this?

  • 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-06-04T01:51:27+00:00Added an answer on June 4, 2026 at 1:51 am

    Sounds to me like you simply need to keep an interface around instead of a delegate.

     public interface IDoThingy
     {
          void DoStuff();
     }
    
     public class IncreaseVolumeThingy : IDoThingy
     {
         public int Volume { get; set; }
    
         public IncreaseVolumeThingy(int volume)
         {
             Volume = volume;
         }
    
         public void DoStuff()
         {
             SomeClass.ChangeMasterVolume(Volume);
         }
     }
    
     public class Command
     {
          protected IDoThingy _thingy = null;
    
          public Command(IDoThingy thingy)
          {
               _thingy = thingy;
          }
    
          public void ExecuteCommand()
          {
              _thingy.DoStuff();
          }
     }
    

    So instead of creating a set of constructors, you simply make some form of factory based on the command specified. If the user is setting up a Increase volume command, then you new an instance of the IncreaseVolumeThingy and store it. When it is serialized, it can be recreated from state without a delegate.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program that's creating a secure directory for user output. This is
I have been making a program that creates multiple CSV's from another source CSV
I'm creating a program where the user has the option of creating their own
I have a program that creates UIViewControllers in a UISplitView depending upon which row
I am creating a program that encrypts/hashs messages I have a list that contains
I want to make it so a user of my Record creating program can
I'm creating a utility program which draws some (user customizable) icons onto other windows
I am creating a program that will run an experiment on a user. It
I am creating an application where a user will input grades and the program
I'm updating a program for my company that requires the user to have SQL

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.