2

I'd like to be able to run a specific function in my DLL from the command line without changing my Main function.

e.g.

If I had the code:

namespace MyApp;

static class Program
{
    public static void Main(string[] args)
    {
        ConsoleWrite("Starting program v" + Version());
        // program logic
    }

    static void PrintVersion() =>
        Console.WriteLine(Version());

    public static int Version() => 2;
}

Then I would run something like:

dotnet exec MyApp.dll -func PrintVersion

Output:

2

Is this possible?

I've looked at the MS docs for the dotnet command and nothing like this appears to be there. But I may be missing something, or there may be another command that I can use.

1
  • 1
    This question is similar to: How to pass arguments in command line using dotnet?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. The answer to that post will tell you how to pass the function name into your DLL from the command line. Your DLL would need to read that and execute the desired function. Commented Sep 5, 2024 at 23:59

1 Answer 1

0

As far as I can tell, this doesn't seem to be possible :/

With respect to the general question, the best workaround is to utilize the string[] args in Main as described in this answer. But that would require modifying the Main function.

However, in my specific application (getting version info) it is possible to load the assembly in PowerShell and get the version info from that.

[System.Reflection.Assembly]::LoadFrom("C:\full\path\to\YourDllName.dll").GetName().Version

(the above snipped was taken from this answer)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.