当前位置:首页 > Windows程序 > 正文

再谈 C# 交互窗口

2021-03-28 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. 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 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=http://www.mamicode.com/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. 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 的开头部分:

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 ()

// 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 语句:

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/69137.html