蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

实测 C# 命名空间/类结构、文件名与类名独立性、大小写敏感及 Main 入口反射

CSHARPWindows x64、.NET Framework 4.x Visual C# 编译器(csc.exe)本地离线环境。ExploreProgramStructure.cs
ExploreProgramStructure.cs149 行
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Text;
using System.Threading;

namespace HelloWorldApplication
{
    public class HelloWorld
    {
        public string Greeting { get; set; }

        public HelloWorld()
        {
            Greeting = "Hello World";
        }

        public string PrintGreeting(TextWriter writer)
        {
            /* 我的第一个 C# 程序 */
            writer.WriteLine(Greeting);
            return writer.ToString().Trim();
        }
    }
}

public class ExploreProgramStructure
{
    private static string JsonEscape(string s)
    {
        if (s == null) return "null";
        StringBuilder sb = new StringBuilder();
        sb.Append('"');
        for (int i = 0; i < s.Length; i++)
        {
            char c = s[i];
            switch (c)
            {
                case '"': sb.Append("\\\""); break;
                case '\\': sb.Append("\\\\"); break;
                case '\b': sb.Append("\\b"); break;
                case '\f': sb.Append("\\f"); break;
                case '\n': sb.Append("\\n"); break;
                case '\r': sb.Append("\\r"); break;
                case '\t': sb.Append("\\t"); break;
                default:
                    if (c < 0x20)
                        sb.Append(string.Format(CultureInfo.InvariantCulture, "\\u{0:x4}", (int)c));
                    else
                        sb.Append(c);
                    break;
            }
        }
        sb.Append('"');
        return sb.ToString();
    }

    private static string MakeCase(string name, string summary, string expectedJson, string actualJson)
    {
        bool passed = string.Equals(expectedJson, actualJson, StringComparison.Ordinal);
        return string.Format(
            CultureInfo.InvariantCulture,
            "{{\"name\":{0},\"summary\":{1},\"expected\":{2},\"actual\":{3},\"passed\":{4}}}",
            JsonEscape(name),
            JsonEscape(summary),
            expectedJson,
            actualJson,
            passed ? "true" : "false"
        );
    }

    public static void Main(string[] args)
    {
        Console.OutputEncoding = Encoding.UTF8;
        Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

        List<string> cases = new List<string>();

        // Case 1: Namespace, class, property, method, and filename independence
        HelloWorldApplication.HelloWorld hw = new HelloWorldApplication.HelloWorld();
        StringWriter sw = new StringWriter();
        string output = hw.PrintGreeting(sw);
        bool filenameDiffersFromClass = !string.Equals("ExploreProgramStructure", typeof(HelloWorldApplication.HelloWorld).Name, StringComparison.Ordinal);
        string actualStruct = string.Format(
            CultureInfo.InvariantCulture,
            "{{\"output\":{0},\"namespace\":{1},\"class_name\":{2},\"filename_independent\":{3}}}",
            JsonEscape(output),
            JsonEscape(typeof(HelloWorldApplication.HelloWorld).Namespace),
            JsonEscape(typeof(HelloWorldApplication.HelloWorld).Name),
            filenameDiffersFromClass ? "true" : "false"
        );
        string expectedStruct = "{\"output\":\"Hello World\",\"namespace\":\"HelloWorldApplication\",\"class_name\":\"HelloWorld\",\"filename_independent\":true}";
        cases.Add(MakeCase(
            "hello_world_namespace_and_class",
            "验证 HelloWorldApplication 命名空间、HelloWorld 类输出 Hello World 且允许文件名与类名不同",
            expectedStruct,
            actualStruct
        ));

        // Case 2: Case sensitivity and comments ignored by compiler
        int count = 10;
        int Count = 25;
        /* count = 999; 块注释内的语句不会被编译执行 */
        // Count = 888; 单行注释同样被编译器忽略
        string actualCase = string.Format(
            CultureInfo.InvariantCulture,
            "{{\"lower\":{0},\"upper\":{1},\"sum\":{2}}}",
            count,
            Count,
            count + Count
        );
        string expectedCase = "{\"lower\":10,\"upper\":25,\"sum\":35}";
        cases.Add(MakeCase(
            "case_sensitivity_and_comments",
            "实测 C# 标识符大小写敏感(count 与 Count 独立)且单行/多行注释被编译器忽略",
            expectedCase,
            actualCase
        ));

        // Case 3: Main entry point and System.Console.WriteLine metadata
        MethodInfo entry = Assembly.GetExecutingAssembly().EntryPoint;
        MethodInfo writeLine = typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) });
        string actualEntry = string.Format(
            CultureInfo.InvariantCulture,
            "{{\"entry_name\":{0},\"writeline_declaring\":{1},\"has_readkey\":{2}}}",
            JsonEscape(entry != null ? entry.Name : ""),
            JsonEscape(writeLine != null ? writeLine.DeclaringType.FullName : ""),
            typeof(Console).GetMethod("ReadKey", Type.EmptyTypes) != null ? "true" : "false"
        );
        string expectedEntry = "{\"entry_name\":\"Main\",\"writeline_declaring\":\"System.Console\",\"has_readkey\":true}";
        cases.Add(MakeCase(
            "main_entry_and_csc_workflow",
            "反射校验程序集入口点 Main 以及 System.Console.WriteLine / ReadKey 方法定义",
            expectedEntry,
            actualEntry
        ));

        string version = ".NET Framework " + Environment.Version.ToString() + " (csc.exe C# 5.0)";
        Console.WriteLine(string.Format(
            CultureInfo.InvariantCulture,
            "{{\"version\":{0},\"cases\":[{1}]}}",
            JsonEscape(version),
            string.Join(",", cases.ToArray())
        ));
    }
}
本例需在 Windows x64、.NET Framework 4.x Visual C# 编译器(csc.exe)本地离线环境。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows x64、.NET Framework 4.x Visual C# 编译器(csc.exe)本地离线环境。

入口文件:projects/csharp-program-structure-lab/ExploreProgramStructure.cs

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 ExploreProgramStructure.cs,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、.NET Framework 4.x Visual C# 编译器(csc.exe)本地离线环境。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:C#

课程关联工程包(登记关联42章) · 含当前章节或实例 · mbskycom202612a1321.zip

下载课程关联工程包:C#

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1349.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习