[Mono-list] Determining the platform at compile and run time

A Rafael D Teixeira rafaelteixeirabr@hotmail.com
Fri, 31 Oct 2003 02:02:04 -0200


Well, if you could elaborate on what happens with this code when running 
within those alternative implementations, we can try to make it work better  
with these two.

But, out of curiosity, do you know why dotgnu doesn't use the extension of 
the PlatformID enumeration as Mono? Or does it?

To be totally fair, my own code inside Bamboo.Prevalence still checks the 
newline string, to guess the OS... so mixing both approaches, we have:

---------------
// How can I tell dynamically what platform my code is running on?
//
// This is one possible approach, contributed by Aleksey Demakov 
(avd@openlinksw.com)
// And tweaked/encapsulated by Rafael Teixeira 
(rafaelteixeirabr@hotmail.com)

using System;

namespace Mono {

	public enum RunningOS { Unix, Windows };
	public enum RunningCLI { Mono, DotNet, Alternative };

	public class Environment {

		public static RunningOS OS {
			get {
				if (System.Environment.NewLine == "\n")
					return RunningOS.Unix;
				else
					return RunningOS.Windows;
			}
	 	}

		public static RunningCLI CLI {
			get {
				Type enumType = typeof(PlatformID);

				if (Enum.IsDefined (enumType, "Unix")) // check if it is Mono
						return RunningCLI.Mono;
				if (System.Environment.NewLine == "\n")  // is it correct on Rotor and 
DotGnu?
					return RunningCLI.Alternative;
				return RunningCLI.DotNet;
			}
	 	}

		public static void Main(String[] parameters) {
			Console.WriteLine("Running in {0} within the {1} CLI", Environment.OS, 
Environment.CLI);
		}

	}
}
---------------

that on RH9/Mono gives

---------------
[rafael@redhat9 desktop]$ mono Mono.Environment.exe
Running in Unix within the Mono CLI
---------------

Does this new code, fare better with those other CLIs?

Best regards,

Rafael Teixeira
Brazilian Polymath
Mono Hacker since 16 Jul 2001
MonoBrasil Founder
English Blog: http://monoblog.blogspot.com/
Brazilian Portuguese Blog: http://monoblog.weblogger.terra.com.br/

>From: Fergus Henderson <fjh@cs.mu.OZ.AU>
>To: A Rafael D Teixeira <rafaelteixeirabr@hotmail.com>
>CC: richard.torkar@htu.se, mono-list@lists.ximian.com,        
>mono-br@redesolbrasil.org
>Subject: Re: [Mono-list] Determining the platform at compile and run time
>Date: Fri, 31 Oct 2003 12:31:43 +1100
>
>On 30-Oct-2003, A Rafael D Teixeira <rafaelteixeirabr@hotmail.com> wrote:
> > Here is a small twist, save it as Mono.Environment.cs and compile as a
> > library to use or as an executable to just test:
> >
> > -------
> > // How can I tell dynamically what platform my code is running on?
> > //
> > // This is one possible approach, contributed by Aleksey Demakov
> > (avd@openlinksw.com)
> > // And encapsulated by Rafael Teixeira (rafaelteixeirabr@hotmail.com)
>
>That code does the wrong thing when running under Rotor or DotGNU.
>
>--
>Fergus Henderson <fjh@cs.mu.oz.au>  |  "I have always known that the 
>pursuit
>The University of Melbourne         |  of excellence is a lethal habit"
>WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.

_________________________________________________________________
MSN Messenger: instale grátis e converse com seus amigos. 
http://messenger.msn.com.br