在上一篇随笔“浅谈 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 的开头部分:
//
// 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 文件中,该文件中的相关内容如下所示:
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 操作系统中运行是很正常的。
园子中的各位大侠有什么建议吗?请在评论中告诉我。谢谢!


浙公网安备 33010602011771号