Cysharp / ConsoleAppFramework

Zero Dependency, Zero Overhead, Zero Reflection, Zero Allocation, AOT Safe CLI Framework powered by C# Source Generator.
MIT License
1.66k stars 95 forks source link

Allow type arguments #121

Closed xPaw closed 5 months ago

xPaw commented 5 months ago

This would allow using readonly fields in the class. Currently to do this I think I would need to have a method to accept the arguments, and then duplicate them in the class.

I'm thinking something like this:

        public static void Main(string[] args)
        {
            var appInstance = ConsoleApp<App>.Run(args);
        }

        public class App
        {
            /// <summary>
            /// Test app.
            /// </summary>
            /// <param name="foo">A</param>
            /// <param name="bar">B</param>
            public App(int foo, int bar)
            {
                Console.WriteLine(foo + bar);
            }
        }

Is this a reasonable idea?

neuecc commented 5 months ago

I don't understand what you want to do; readonly fields?

xPaw commented 5 months ago

I want to pass a constructor instead of method into Run() so that it creates an instance of a class

neuecc commented 5 months ago

Okay, I don't think it's a good idea, so I can't adopt that idea. If you want a return value, you can pass it out through a closure or something.

Foo foo = default!;
ConsoleApp.Run(args, (int x, int y) => 
{
    foo = new Foo();
});

Console.WriteLine(foo);
xPaw commented 5 months ago

I don't really want the return value, but rather for the parsed arguments to be passed into the constructor.

neuecc commented 5 months ago

foo = new Foo(x, y);.