Indycate
Indycate

Reputation: 11

Accessing objects from a c# console app externally

I'm a new junior dev and working on my first solo project. For that im creating a c# console app for automated word document creation. The plan is to integrate the app in different projects and different invironments thats why it should be an stand alone solution. But in order to give it a little more felxability and versatility I want the app to give some output.

Because of the different invironments the app should be useable with e.g. a python script aswell. What would be the best practice to do so??? especially if you want to give diffrent outputs depending on the use case???

A litte example: I'm getting an json object with all the information to create a word document. In the stand alone solution I don't need an output (it only creats a word doc and pfd). But if I want to use the app it in an wpf UI project were i want preview and live update functionality it would be nice to have access to the created objects. In order to achieve this would I work with a return value (exit code) and (depending on the code) create the desired objects again, or would I embed it as a library and just call all the normal functionality, or could i even do something like an out value or complex return value (like string[]) to return a serialized object?

a.)

static int Main(string[] args)
{
   //do something
   return *code for decision making*
}

b.) create library

c.)

static void Main(string[] args, out string[] output)
{
   //do something
   output = *serialized object(s)*
}

or

static string[] Main(string[] args)
{
   //do something
   return *serialized object(s)*
}

Upvotes: 1

Views: 56

Answers (1)

user11760346
user11760346

Reputation:

According to this MSDN article, the Main() method in C# can only have these signatures:

public static void Main();
public static int Main();
public static void Main(string[] args);
public static int Main(string[] args);
public static async Task Main();
public static async Task<int> Main();
public static async Task Main(string[] args);
public static async Task<int> Main(string[] args);

This shows that it's impossible to return data about an object from the Main() method in C#.

However, you can return data about an object for another environment/program to handle by printing the data about that object to the console output stream in a format like JSON. From your other environment (ex: Python) you can read the console output stream of your C# program as a string and then parse it.

Upvotes: 1

Related Questions