Why is the entry point allowed as confidential?

How it works? I thought Main should have been "summoned." But how is this possible if it is marked as private?

 public class Program { private static void Main() { } } 
+2
c #
Mar 18 '12 at 18:15
source share
5 answers

From Jon Skeet on bytes.com:

In principle, the execution of the main method is started by special code in the CLR (or, perhaps, the code that controls the CLR to start with), which does not need to obey the same rules.

Source

In addition, there is another question that already covers this topic.

+8
Mar 18 '12 at 18:17
source share

Following the MSDN , the main method should not be publicly available:

Main is declared inside a class or structure. The home should be static and it should not be publicly available . (In an earlier example, it gains private access by default.) A nested class or structure does not have to be static.

+3
Mar 18 '12 at 18:18
source share

This is a detail of the language, the CLR simply reads the EntryPointToken value from the assembly header and does not check the availability of the method with this token. The main call is _AppDomain.ExecuteAssembly (). Therefore, we need to refer to the C # language specification, section 3.1 explicitly states the accessibility rule:

In C #, each method must be defined as a member of a class or structure. Typically, the declared availability (§3.5.1) of a method is determined by the access modifiers (§10.3.5) specified in its declaration, and similarly, the declared type availability is determined by the access modifiers specified in its declaration. In order for this method of this type to be available for invocation, both the type and the member must be available. However, the entry point of the application is a special case. In particular, the runtime can access the application entry point regardless of its declared availability and regardless of the declared availability of its type inclusion declarations .

The bold section documents what the CLR does with the EntryPointToken. The C # compiler can check availability if it wants to, but doesn’t.

+2
Mar 18 '12 at 19:18
source share

The main method is executed by the CLR, when you ever execute your CLR code compiler, it searches for this main method. Even if you give the main letter in small letters, it will not be called.

0
Mar 18 '12 at 18:22
source share

Adds modifiers to .Net (really strong) sentences. You can call any method or access any property / field using reflection. Consider code that behaves like what actually happens when main is called.

 public class EntryPointAttribute : System.Attribute { public string EntryPoint { get; private set; } public EntryPointAttribute(string entryPoint) { this.EntryPoint = entryPoint; } } public static class EntryPointProcessor { public static void Process(object theObject) { Type t = theObject.GetType(); var ep = t.GetCustomAttributes(typeof(EntryPointAttribute), true).FirstOrDefault(); string entryPointName = ((EntryPointAttribute)ep).EntryPoint; MethodInfo mi = t.GetMethod(entryPointName, BindingFlags.Static | BindingFlags.NonPublic); mi.Invoke(null, new object[0] { }); } } [EntryPoint("anentrypoint")] public class entryPointClass { private static void anentrypoint() { Console.WriteLine("in anentrypoint"); } } class Program { static void Main(string[] args) { EntryPointProcessor.Process(new entryPointClass()); } } 
0
Mar 18 '12 at 18:43
source share



All Articles