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

  • Home
  • SEARCH
  • 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 8956637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:49:54+00:00 2026-06-15T14:49:54+00:00

For a server I am writing, I have a folder of files, one class

  • 0

For a server I am writing, I have a folder of files, one class in each file.
Each class represent a request action from the client.
If I add a new action, I want to simply be able to add a new file, letting the file register itself.

In language such as Go you can have init functions, one in each file, that runs at initialization, letting you register things like factory delegates (or first class functions).

Can you achieve something similar in C#, letting the files register their own classes without you having to edit a second file containing a list of all registered actions?

// This won't work, but how to do it?
func init() {
    // Registering a factory function to a Dictionary<string, Func<IAction>>
    Reg.ClassDictionary.Add("connect", () => { return new Connect(); });
}

namespace Action
{
    class Connect : IAction
    {
         [JsonProperty("user")]
        public string Username;

        [JsonProperty("pass")]
        public string Password;

        public bool Exec()
        {
            return ConnectToServer(Username, Password);
        }
    }
}
  • 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-15T14:49:55+00:00Added an answer on June 15, 2026 at 2:49 pm

    If it’s a console app, just do it at the start of your Main method.

    If it’s a windows service, when you extend ServiceBase, you get an OnStart method (that you can override) which executes when the service starts. http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicebase.onstart.aspx

    Here’s a quick tutorial on Windows Services to get you started: http://www.switchonthecode.com/tutorials/creating-a-simple-windows-service-in-csharp

    EDIT: Based on your clarification, AFAIK, there’s no built-in way to have files/types do what you’re asking. However, what you could do is some reflection. You can peruse your assemblies for particular types/interfaces that implement your IAction (or rather, some other descriptive type). You can search through for all of them, instantiate them, then call their various init methods during the startup phases I described above.

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

Sidebar

Related Questions

I'm writing chat client/server app with c# and I have problem with threading. I
I have a year's experience writing client code but none with server stuff. I
I have an application I’m writing in access with a SQL server backend. One
I have image file placed on the one server and application on other server.
I am writing a Happstack server and I have a MongoDB database to connect
I have rails application running in linode server .Some guy is continuously spamming (writing
I am writing a SIP server, and I have it taking calls and then
I am writing a little application in android which would have a server part
I am writing a stored procedure in SQL Server 2008. Here I have a
i have this script: BACKUP DATABASE timeclockplus TO DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL.2\MSSQL\Backup\backup.bak'

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.