Note that the purpose of this article is not to provoke a language dispute. Although there is a vindication for C #, it is mainly about mono and simple testing.
updated:25th August 2012
Updated "compile Once, Run Anywhere: The ultimate cross-platform goal" section.
Conmajia 2012
Introduction
"Write once, run Anywhere" (once written, running everywhere, Wora), sometimes written "write once, run Everywhere" (wore), is sun Microsystem (acquired by Oracle in 2010) is a slogan for promoting the cross-platform nature of the Java language. Ideally--often impossible--compiling a program written in the Java language into standard bytecode (bytecode) can run on any device that supports a Java Virtual machine (JVM).
Many dabbler Java "Experts" often use this to run. NET users, said they were "kidnapped by Microsoft, only the JVM this industry standard can cross the platform."
What is the real situation? On the one hand, real Java developers are constantly complaining about "write once, Debug Anywhere" (once written, debugged everywhere), on the other hand, more and more people realize. NET is essentially cli/cts, the industry standard, and the CLR is also a virtual machine. So, it is always a "cross-platform" ability to highlight Java and belittle. NET, is already outdated and do not understand the performance.
I recently deleted windows and installed Linux Mint (an Ubuntu-based Linux distribution) because my computer was running slowly.
650) this.width=650; "src=" Http://pic002.cnblogs.com/images/2012/407692/2012082205330133.png "style=" border:0px; " />
In the Linux environment, there are very famous. NET run-time--mono.
The name of Mono, and the friends who make. NET know that. It makes. NET program has the possibility of running across platforms under Linux. Mono currently supports. NET v4.0, which has gradually stabilized and became popular (see Compatibility section). Since I only have C # (ashamed), I need to develop and run the. NET program under Linux and then install Mono.
$ sudo apt-get install Mono-gmcs libmono-system-data2.0-cil libmono-system-ldap2.0-cil Libmono-system-messaging2.0-cil Libmono-system-runtime2.0-cil
Here's an off-topic. Although for technologies that are already widely used, such as. NET), the size of the file at run time is not much of a discussion, but someone still has to say this to illustrate how the. NET Framework is bad (in fact, after win Vista this is no longer a thing). So what about Mono's performance?
Mono has a full installation size of 78MB (Java Minimum installation size 95MB), while mono minimizes installation requires 7MB. (Ref: Http://www.infoq.com/cn/news/2007/07/Mono-Runtime-Size)
In order to facilitate development, I installed the monodevelop directly. This is the Linux version of the famous open source. NET IDE sharpdevelop on Windows.
650) this.width=650; "src=" Http://pic002.cnblogs.com/images/2012/407692/2012082205433259.png "style=" border:0px; " />
The installation commands are as follows:
$ sudo apt-get install MonoDevelop
Linux under compilation
Here are a few simple program tests. Note that the program code here is fully operational under Windows.
Command-line Programs
650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" border:0px; "/>
1 using System; 2 3 Namespace Test 4 {5 class program 6 {7 static void Main () 8 {9 Console.writel INE ("Hello mono!"); Console.ReadLine (); 11}12}13}
650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" border:0px; "/>
Run results
650) this.width=650; "src=" Http://pic002.cnblogs.com/images/2012/407692/2012082205490582.png "style=" border:0px; " />
WinForm Program
650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" border:0px; "/>
1 using System; 2 using System.Windows.Forms; 3 4 Namespace test 5 { 6 public class mainform:form 7 { 8 TextBox textBox1; 9 Button button1;10 public mainform () 11 {12 textbox1=new textbox ();13 textbox1.text= "Text here ...";14 textbox1.location=new system.drawing.point (10,10); 15 button1=new button ();16 button1. Text= "Click me."; 17 button1. Autosize=true;18 button1. Location=new system.drawing.point (10,40);19 this. controls.add (TextBox1);20 This. controls.add (button1);21 } 22 }23 }
650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" border:0px; "/>
Run results
650) this.width=650; "src=" Http://pic002.cnblogs.com/images/2012/407692/2012082206023780.png "style=" border:0px; " />
Isn't that a surprise? Under Linux, you can run the WinForm program directly. It's so convenient. The demo code is compiled under Linux and does not prove "Write once, run anywhere", so what if I run the exe compiled by Windows directly? Let's try out the ultimate goal of the cross-platform of compiled programs: Compile Once,run Anywhere
Compile Once, Run Anywhere: The ultimate cross-platform goal
Here's what I did before windows with Visual Studio and SharpDevelop compiled EXE without any processing (nor processing) to run directly.
First of all, "Hive wars", let's look at the effects of running under Windows.
650) this.width=650; "src=" http://pic002.cnblogs.com/images/2012/407692/2012082206042975.jpg "style=" border:0px; " />
Then it runs under Linux.
Note: Because the default. exe is associated with the archive manager, you need to select Open with "Mono Runtime".
650) this.width=650; "src=" Http://pic002.cnblogs.com/images/2012/407692/2012082206073034.png "style=" border:0px; " />
Run the following effect
650) this.width=650; "src=" Http://pic002.cnblogs.com/images/2012/407692/2012082206125861.png "style=" border:0px; " />
Tested for various functions normally. Indicates that GDI + works correctly, and that controls such as ToolStrip are functioning normally.
Take a look at another program I recently published: "InvokeHelper".
This is what happens under Windows.
650) this.width=650; "src=" Http://pic002.cnblogs.com/images/2012/407692/2012082206401378.png "style=" border:0px; " />
This is the effect of running in the mono environment
650) this.width=650; "src=" Http://pic002.cnblogs.com/images/2012/407692/2012082206281396.png "style=" border:0px; " />
The description and thread-related features work fine.
Again, it's related to Windows API. In fact, it is impossible to use the toes (not only C #, any language is the same, the Peace Platform API strongly related, how can "cross-platform" it).
"Get System Icon", this program uses shgetfileinfo this Windows API:
650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" border:0px; "/>
1 [DllImport ("Shell32.dll")]2 static extern int Shgetfileinfo (3 string pszpath,4 uint dwfileattributes,5 ref shfil Einfo psfi,6 UINT cbfileinfo,7 uint UFLAGS8);
650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" border:0px; "/>
Working properly in Windows
650) this.width=650; "src=" http://pic002.cnblogs.com/images/2012/407692/2012082206332095.jpg "style=" border:0px; " />
What about under Linux? Run the next try:
650) this.width=650; "src=" Http://pic002.cnblogs.com/images/2012/407692/2012082206334851.png "style=" border:0px; " />
Calling the Open File dialog box is OK, but will automatically exit once the Windows API is run. Therefore, after the platform, the Peace Station (WIN) related API is not used, it is also a matter of course. C # and Java have no way to jump off such a fate (laughter).
Compatibility
Here's an example of some of the current compatibility scenarios for Mono: Supported Paradigms (2.0+) and Var (3.0+).
650) this.width=650; "src=" Http://img.my.csdn.net/uploads/201208/22/1345596760_3932.png "style=" border:0px; "/>
The official compatibility can be viewed on this page: http://www.mono-project.com/Compatibility
Currently the newest mono is 2.10.8. (Released December 19th, 2011) can already support the. NET 4.0 version. See:
650) this.width=650; "src=" Http://pic002.cnblogs.com/images/2012/407692/2012082209231014.png "style=" border:0px; " />
Transplantation
Choose a different platform, sooner or later to face the transplant problem. Since cli/cts only specifies the underlying part of the language, there are some differences in the implementation of each runtime (see previous section: Compatibility). So, Mono officially provides a transplant helper called mono migration Analyzer (MOMA, MO). This tool can directly tell you the possibility of porting a ready-made Windows + Microsoft.NET-based program to Win/linux/mac + mono.
650) this.width=650; "src=" Http://pic002.cnblogs.com/images/2012/407692/2012082209472518.png "style=" border:0px; " />
Sometimes the implementation of a small function, there are many ways to implement, but some of the implementation is dependent on the Windows API, and some not, without compromising performance, we prefer to choose the standard implementation rather than the special implementation. This is the recipe for success using mono as a project.
Summarize
At present, the more famous non-Windows platform under the. NET Virtual machine/runtime only mono, portable.net (thanks @ Crane Soaring), I believe that over time, there will be more runtime appear, mono will become more powerful. When it comes, it's more than just java,c# and. NET platform of various languages (VB, C + +/CLI, F #, etc.) can be implemented "Write once, run anywhere". Of course, there's the "Debug Anywhere" (laughter) that comes with it.
Finish
Conmajia 2012
Write Once, Run Anywhere: This is not Java, this is C #