简体   繁体   中英

C# : Calling a Method out of console application

I have a console app, that is used for importing stuff from a WS.

I'm really new to Console Apps so I have a question.

The Project contains a class called Importer.cs. This class has a Method called Initialize():

class Importer
{
    static void  Initialize()
    {
        //here i connect to the server etc.
    }
}

Now I want to be able to call my APP like:

Importer.exe Initialize

So it should call the Initialize method, and then I would like to be able to go on with for example:

Importer.exe StartImport

I already work with the args[] parameter, but i'm quite stuck now.

使用CommandLineParser之类的库,然后使用Reflection将这些方法作为MethodInfo对象调用。

In your console application, look in your solution explorer (in the right window of VS where all your files are shown). Find the one called PROGRAM.CS. Open it and look for this:

  static void Main(string[] args)
    {

    }

Inside that, put this:

    static void Main(string[] args)
    {
       //This starts a new instance of the Importer Class
       Importer myImporter = new Importer();
       //This calls the Initialize Method within Importer
       Importer.Initialize();
       //This calls the StartInput() Method within Importer
       Importer.StartInput();    
       //Use Console.ReadLine() as the last line in this method to keep your screen open after program execution ends.
       Console.ReadLine();       
    }

The first thing your console app runs ins Main() in console app.

Then, you simply test the project using F5. After testing, build the project and run by double-clicking the EXE file.

Depending on what you're exactly doing, this might not even work. I imagine a situation, where you Initialize() and afterwards StartImport() , but in between these 2 calls the program has been finished and the initialized state is gone. If you do not have methods like Initialize() , but rather atomic subcommands, your approach is possible in principle.

But there arises one question: Is this going to be generic? I mean: Say, you add another method. Do you then want to have the access to the method by a subcommand automatically established or wouldn't you mind adding another subcommand case? The first option would mean, you need to get comfortable with Reflection.

Now, say, you add methods, which you cannot declare private due to whatever reason, but you don't want them to be exposed as a subcommand. So you would need to keep track of the visibility. Sure, this would mean poor design, but if you're stuck with legacy components, this might just happen.

Now, let's say you need the Initialize() (or similar) command(s). They take care of a connection state or whatever and the program needs to still run, when the next subcommand is invoked, so it can use the initialized information, handles, etc.. Wouldn't it then make more sense to design your console application like a shell? So you would start with Importer.exe and get asked for subcommands.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM