再谈 C# 交互窗口

时间:2022-08-31 07:57:48

在上一篇随笔“浅谈 C# 交互窗口”中,我提到在 mono 中可以使用 csharp 交互执行 C# 代码。如果想在 Windows 操作系统中使用 C# 交互窗口,而又不想在 Windows 操作系统中安装 mono 的话,可以考虑将 csharp.exe 移植到 Windows 操作系统中。

Ubuntu 10.10 操作系统中自带的是 mono 2.6.7:

ben@ben-m4000t:~$ cat /etc/issue
Ubuntu 10.10 \n \l

ben@ben-m4000t:~$ mono --version
Mono JIT compiler version 2.6.7 (Debian 2.6.7-3ubuntu1)
Copyright (C) 2002-2010 Novell, Inc and Contributors. www.mono-project.com
	TLS:           __thread
	GC:            Included Boehm (with typed GC and Parallel Mark)
	SIGSEGV:       altstack
	Notifications: epoll
	Architecture:  amd64
	Disabled:      none
ben@ben-m4000t:~$ gmcs --version
Mono C# compiler version 2.6.7.0
ben@ben-m4000t:~$ csharp --version
Mono C# compiler version 2.1.0.0
ben@ben-m4000t:~$ which csharp
/usr/bin/csharp
ben@ben-m4000t:~$ cat /usr/bin/csharp
#!/bin/sh
exec /usr/bin/mono $MONO_OPTIONS /usr/lib/mono/2.0/csharp.exe "$@"
ben@ben-m4000t:~$ 

目前最新版本是 mono 2.8.1。因此,在 Ubuntu 10.10 操作系统中安装 mono 2.8.1 :

ben@ben-m4000t:~/src$ wget http://ftp.novell.com/pub/mono/sources/mono/mono-2.8.1.tar.bz2
ben@ben-m4000t:~/src$ tar xjf mono-2.8.1.tar.bz2
ben@ben-m4000t:~/src$ rm mono-2.8.1.tar.bz2
ben@ben-m4000t:~/src$ cd mono-2.8.1
ben@ben-m4000t:~/src/mono-2.8.1$ ./configure --prefix=/opt/mono-2.8.1
ben@ben-m4000t:~/src/mono-2.8.1$ make
ben@ben-m4000t:~/src/mono-2.8.1$ sudo make install

然后看看安装后的效果:

ben@ben-m4000t:~/src/mono-2.8.1$ cd /opt/mono-2.8.1
ben@ben-m4000t:/opt/mono-2.8.1/bin$ ./mono --version
Mono JIT compiler version 2.8.1 (tarball 2010年 12月 01日 星期三 08:50:53 CST)
Copyright (C) 2002-2010 Novell, Inc and Contributors. www.mono-project.com
	TLS:           __thread
	SIGSEGV:       altstack
	Notifications: epoll
	Architecture:  amd64
	Disabled:      none
	Misc:          debugger softdebug 
	LLVM:          supported, not enabled.
	GC:            Included Boehm (with typed GC and Parallel Mark)
ben@ben-m4000t:/opt/mono-2.8.1/bin$ ./dmcs --version
Mono C# compiler version 2.8.1.0
ben@ben-m4000t:/opt/mono-2.8.1/bin$ ./csharp --version
Mono C# compiler version 4.0.0.0
ben@ben-m4000t:/opt/mono-2.8.1/bin$ cat csharp
#!/bin/sh
exec /opt/mono-2.8.1/bin/mono $MONO_OPTIONS /opt/mono-2.8.1/lib/mono/4.0/csharp.exe "$@"

然后把 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:

C:\bin\CsharpRepl> csharp

未经处理的异常:  System.IO.FileNotFoundException: 未能加载文件或程序集
“Mono.CSharp, Version=4.0.0.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756”
或它的某一个依赖项。系统找不到指定的文件。
   在 Mono.Driver.Startup(String[] args)
   在 Mono.Driver.Main(String[] args)

哎呀,运行出错了。还需要一个 Mono.CSharp.dll 才能运行。让我们来找出这个 Mono.CSharp.dll 吧:

ben@ben-m4000t:/opt/mono-2.8.1$ find . -name Mono.CSharp.dll
./lib/mono/4.0/Mono.CSharp.dll
./lib/mono/2.0/Mono.CSharp.dll
./lib/mono/gac/Mono.CSharp/4.0.0.0__0738eb9f132ed756/Mono.CSharp.dll
./lib/mono/gac/Mono.CSharp/2.0.0.0__0738eb9f132ed756/Mono.CSharp.dll
ben@ben-m4000t:/opt/mono-2.8.1$ ll lib/mono/4.0/Mono.CSharp.dll
lrwxrwxrwx 1 root root 60 2010-12-01 09:07 lib/mono/4.0/Mono.CSharp.dll
     -> ../gac/Mono.CSharp/4.0.0.0__0738eb9f132ed756/Mono.CSharp.dll
ben@ben-m4000t:/opt/mono-2.8.1$ ll lib/mono/gac/Mono.CSharp/4.0.0.0*/Mono.CSharp.dll
-rwxr-xr-x 1 root root 1105408 2010-12-01 09:07
 lib/mono/gac/Mono.CSharp/4.0.0.0__0738eb9f132ed756/Mono.CSharp.dll
ben@ben-m4000t:/opt/mono-2.8.1$ 

因此,把 Ubuntu 操作系统中上述 gac 目录中的 Mono.CSharp.dll 拷贝到 Windows 操作系统中,再次运行 csharp.exe :

C:\bin\CsharpRepl> dir
 驱动器 C 中的卷没有标签。
 卷的序列号是 F0AA-2E0D

 C:\bin\CsharpRepl 的目录

2010-12-01  10:13              .
2010-12-01  10:13              ..
2010-12-01  09:07            37,376 csharp.exe
2010-12-01  09:07         1,105,408 Mono.CSharp.dll
               2 个文件      1,142,784 字节
               2 个目录  9,746,513,920 可用字节

C:\bin\CsharpRepl> csharp

C:\bin\CsharpRepl>

这次运行没有出错,而是直接退出了。这更糟糕了。只好找到 csharp.exe 的 C# 源程序看看发生了什么。

ben@ben-m4000t:/opt/mono-2.8.1/bin$ cd ~/src/mono-2.8.1
ben@ben-m4000t:~/src/mono-2.8.1$ find . -name csharp
./mcs/tools/csharp
./scripts/csharp
ben@ben-m4000t:~/src/mono-2.8.1$ cd mcs/tools/csharp
ben@ben-m4000t:~/src/mono-2.8.1/mcs/tools/csharp$ ll
总计 56
-rw-r--r-- 1 ben ben  2434 2010-11-12 18:32 ChangeLog
-rw-r--r-- 1 ben ben    60 2010-11-12 18:24 csharp.exe.sources
-rw-r--r-- 1 ben ben 22963 2010-11-12 18:24 getline.cs
-rw-r--r-- 1 ben ben   672 2010-11-12 18:24 Makefile
-rw-r--r-- 1 ben ben 16749 2010-11-12 18:24 repl.cs
ben@ben-m4000t:~/src/mono-2.8.1/mcs/tools/csharp$ 

上面的 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 语句:

再谈 C# 交互窗口再谈 C# 交互窗口代码
   
   
   
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);
}

然后重新编译:

ben@ben-m4000t:~/src/mono-2.8.1$ make
ben@ben-m4000t:~/src/mono-2.8.1$ sudo make install

再将重新编译后的 csharp.exe 拷贝到 Windows 操作系统中运行:

C:\bin\CsharpRepl> csharp
System.TypeLoadException: 重写成员
“Mono.CSharp.StreamReportPrinter.Print(Mono.CSharp.AbstractMessage)”
时违反了继承安全性规则。重写方法的安全可访问性必须与所重写方法的安全可访问性匹配。
   在 Mono.CSharp.Evaluator.InitAndGetStartupFiles(String[] args)
   在 Mono.Driver.Startup(String[] args)

C:\bin\CsharpRepl>

运行时出错信息如上所示。经查,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 操作系统中运行是很正常的。

园子中的各位大侠有什么建议吗?请在评论中告诉我。谢谢!