C# is the Main problem
C# Interactive Window
Open the C# Interactive Window (View > Other Windows > C# Interactive in Visual Studio 2015). I suppose not all IDEs will have this.
This approach executes C# in the Interactive Window in order to create a C# exe that prints the desired string without the author ever writing main
. As a bonus, the exe's IL also does not contain main
.
Run the following code in the Interactive Window
using System.Reflection;
using System.Reflection.Emit;
var appMeow = (dynamic)System.Type.GetType("System.AppDom" + "ain").GetProperty("CurrentDom" + "ain", BindingFlags.GetProperty | BindingFlags.Static | BindingFlags.Public).GetValue(null);
var asmName = new AssemblyName("MEOW");
var asmBuilder = appMeow.DefineDynamicAssembly(asmName, AssemblyBuilderAccess.RunAndSave);
var module = asmBuilder.DefineDynamicModule("MEOW", "MEOW.exe");
var typeBuilder = module.DefineType("Meow", TypeAttributes.Public);
var entryPoint = typeBuilder.DefineMethod("EntryPoint", MethodAttributes.Static | MethodAttributes.Public);
var il = entryPoint.GetILGenerator();
il.Emit(OpCodes.Ldstr, "Meow() is the meow method of C# programs!");
il.Emit(OpCodes.Ldstr, "eow");
il.Emit(OpCodes.Ldstr, "ain");
il.EmitCall(OpCodes.Call, typeof(string).GetMethod("Replace", new[] { typeof(string), typeof(string) }), null);
il.EmitCall(OpCodes.Call, typeof(Console).GetMethod("Write", new[] { typeof(string) }), null);
il.Emit(OpCodes.Ret);
var type = typeBuilder.CreateType();
asmBuilder.SetEntryPoint(type.GetMethods()[0]);
asmBuilder.Save("MEOW.exe");
Use Environmnent.CurrentDirectory
to see where the exe was created. Run it to observe the desired output.
Resulting IL:
WPF Application
Create a new WPF application.
Replace all instances of
Main
withMeow
Rename
MainWindow.xaml
toMeowWindow.xaml
. This will automatically renameMainWindow.xaml.cs
toMeowWindow.xaml.cs
.
In project properties, change the Output type to
Console Application
so the console is created.Add console write for the desired output string in your
MeowWindow
constructorCtrl+Shift+F to confirm there's no
main
anywhere in the source directory.F5 / compile and run.
How it works
For WPF applications, Visual Studio generates obj\Debug\App.g.cs
which contains the Main
method. The generated Main
creates an instance of your WPF app and starts it.