在上一篇随笔“浅谈 C# 交互窗口”中,我提到在 mono 中可以使用 csharp 交互执行 C# 代码。如果想在 Windows 操作系统中使用 C# 交互窗口,而又不想在 Windows 操作系统中安装 mono 的话,可以考虑将 csharp.exe 移植到 Windows 操作系统中。
Ubuntu 10.10 操作系统中自带的是 mono 2.6.7:
目前最新版本是 mono 2.8.1。因此,在 Ubuntu 10.10 操作系统中安装 mono 2.8.1 :
然后看看安装后的效果:
然后把 Ubuntu 10.10 操作系统中的 /opt/mono-2.8.1/lib/mono/4.0/csharp.exe 文件拷贝到安装了 Microsoft .NET Framework 4 的 Microsoft Windows Server 2003 R2 Enterprise Edition Service Pack2 操作系统中,运行 csharp.exe:
哎呀,运行出错了。还需要一个 Mono.CSharp.dll 才能运行。让我们来找出这个 Mono.CSharp.dll 吧:
因此,把 Ubuntu 操作系统中上述 gac 目录中的 Mono.CSharp.dll 拷贝到 Windows 操作系统中,再次运行 csharp.exe :
这次运行没有出错,而是直接退出了。这更糟糕了。只好找到 csharp.exe 的 C# 源程序看看发生了什么。
上面的 repl.cs 就是 csharp.exe 的 C# 源程序之一,其中包含 Main 方法。下面是 repl.cs 的开头部分:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | // // repl.cs: Support for using the compiler in interactive mode (read-eval-print loop) // // Authors: // Miguel de Icaza (miguel@gnome.org) // // Dual licensed under the terms of the MIT X11 or GNU GPL // // Copyright 2001, 2002, 2003 Ximian, Inc (http://www.ximian.com) // Copyright 2004, 2005, 2006, 2007, 2008 Novell, Inc // // // TODO: // Do not print results in Evaluate, do that elsewhere in preparation for Eval refactoring. // Driver.PartialReset should not reset the coretypes, nor the optional types, to avoid // computing that on every call. // using System; using System.IO; using System.Text; using System.Globalization; using System.Collections; using System.Reflection; using System.Reflection.Emit; using System.Threading; using System.Net; using System.Net.Sockets; using System.Collections.Generic; using Mono.CSharp; namespace Mono { public class Driver { static int Main ( string [] args) { #if !ON_DOTNET if (args.Length > 0 && args [0] == "--attach" ) { new ClientCSharpShell (Int32.Parse (args [1])).Run ( null ); return 0; } if (args.Length > 0 && args [0].StartsWith ( "--agent:" )) { new CSharpAgent (args [0]); return 0; } #endif return Startup(args); } static int Startup ( string [] args) { string [] startup_files; try { startup_files = Evaluator.InitAndGetStartupFiles (args); Evaluator.DescribeTypeExpressions = true ; Evaluator.SetInteractiveBaseClass ( typeof (InteractiveBaseShell)); } catch { return 1; } return new CSharpShell ().Run (startup_files); } } } |
问题就出在第 52 行到第 65 行的 Startup 方法中。因此在该方法中增加一些 Debug 语句:

static int Startup (string[] args)
{
string[] startup_files;
try {
startup_files = Evaluator.InitAndGetStartupFiles (args);
Evaluator.DescribeTypeExpressions = true;
Evaluator.SetInteractiveBaseClass (typeof (InteractiveBaseShell));
} catch (Exception ex) {
// Begin Add by Ben (http://www.cnblogs.com/skyivben)
Console.WriteLine(ex);
// End Add by Ben (Wed 2010-12-01)
return 1;
}
return new CSharpShell ().Run (startup_files);
}
然后重新编译:
再将重新编译后的 csharp.exe 拷贝到 Windows 操作系统中运行:
运行时出错信息如上所示。经查,Mono.CSharp 命名空间中的 StreamReportPrinter 类继承自同一命名空间中的 ReportPrinter 类,这两个类的源程序代码均在 src/mono-2.8.1/mcs/mcs/report.cs 文件中,该文件中的相关内容如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | namespace Mono.CSharp { // // Generic base for any message writer // public abstract class ReportPrinter { /// <summary> /// Whether to dump a stack trace on errors. /// </summary> public bool Stacktrace; int warnings, errors; public int WarningsCount { get { return warnings; } } public int ErrorsCount { get { return errors; } } protected virtual string FormatText ( string txt) { return txt; } // // When (symbols related to previous ...) can be used // public virtual bool HasRelatedSymbolSupport { get { return true ; } } public virtual void Print (AbstractMessage msg) { if (msg.IsWarning) ++warnings; else ++errors; } protected void Print (AbstractMessage msg, TextWriter output) { StringBuilder txt = new StringBuilder (); if (!msg.Location.IsNull) { txt.Append (msg.Location.ToString ()); txt.Append ( " " ); } txt.AppendFormat ( "{0} CS{1:0000}: {2}" , msg.MessageType, msg.Code, msg.Text); if (!msg.IsWarning) output.WriteLine (FormatText (txt.ToString ())); else output.WriteLine (txt.ToString ()); if (msg.RelatedSymbols != null ) { foreach ( string s in msg.RelatedSymbols) output.WriteLine (s + msg.MessageType + ")" ); } } } class StreamReportPrinter : ReportPrinter { readonly TextWriter writer; public StreamReportPrinter (TextWriter writer) { this .writer = writer; } public override void Print (AbstractMessage msg) { Print (msg, writer); base .Print (msg); } } } |
从上面的源程序代码中,我看不出为什么在 Windows 操作系统中运行时会出现“重写成员 ... 时违反了继承安全性规则。重写方法的安全可访问性必须与所重写方法的安全可访问性匹配。”的错误。这个 csharp.exe 程序在 Linux 操作系统中运行是很正常的。
园子中的各位大侠有什么建议吗?请在评论中告诉我。谢谢!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述