Calling mono c# code from Microsoft .net? - c#

I have some neural net code written in c# that would benefit from using SIMD support. Mono 2.2 just came out that supports SIMD but Microsoft's c# does not support this yet. Being happy with my c# setup I was wondering if I could write a lib in mono for that piece and call it from .net.
Edit:
I guess what I really want to know is it possible to compile mono down to something like a DLL that I then can call from dotnet. I heard Miguel de Icaza on a podcast saying that for the iphone the mono compiler would allow them to compile down to an exe for moonlight so it did not violate the terms of service for iphone so it got me thinking what else can you compile to.
I heard Miguel de Icaza on another pod cast Herding Code Episode 28 say that you could use the mono complier to compile to an exe not just to intermediate code. What are the implications of this?
This got my curiosity up so I thought that I would throw a bounty at it.

From Miguel de Icaza's blog:
Our library provides C# fallbacks for
all of the accelerated instructions.
This means that if your code runs on a
machine that does not provide any SIMD
support, or one of the operations that
you are using is not supported in your
machine, the code will continue to
work correctly.
This also means that you can use the
Mono.Simd API with Microsoft's .NET on
Windows to prototype and develop your
code, and then run it at full speed
using Mono.
As I understand it, this means that you can write code that uses Mono.Simd, and will be able to run it under .Net, but it won't be any faster than regular code, because the .Net runtime doesn't support SIMD yet.

Essentially, if you write it with Simd and distribute the dll with your code, it will use acceleration if the target VM supports it. If not, it doesn't break. So you can use the library and give any users of your program who run .NET apps with Mono a speed boost.
Microsoft has been said to be planning to add such support in its next release of its runtime, though I cannot find the link and don't have it handy right this sec---can dig the link out of a historic backup if anyone is interested enough.

In order to take advantage of SIMD features, the runtime should be able to natively support it. Basically, Mono treats Mono.Simd namespace specially in the runtime. Obviously, Microsoft .NET runtime does not support this feature. However, the Mono.Simd assembly provided is a completely valid and normal .NET assembly written in managed code and therefore it can run on .NET CLR, but it would be just a software emulation of what SIMD instructions do.
You can run Mono runtime on Windows and take advantage of those features but there is no direct way to run half of application on .NET and the other half on Mono (you could, of course, use communication mechanisms as two distinct applications can use, but it doesn't make sense for this scenario at all).

Related

Is Roslyn cross platform?

I've been looking at Roslyn for quite some time now, and I'm curious and excited about it. One thing I noticed is that they mentioned that the compiler is re-written in managed code. This raises the question of whether Roslyn is able to run on non-.NET virtual machines, such as Mono.
I would really love to embed C# scripting using Roslyn in my video games, and to use many of their other features in my applications, but I'm wondering if using Roslyn will break the ability for it to run on Mono.
Has anyone tried running Roslyn on Mono? Is it possible? Why or why not?
To clarify, I'm interested in both whether the managed assembly can run on Mono, and whether it can generate assemblies that mono can run.
Despite it being the furthest thing from Eric's mind, Roslyn has been released as true Open Source (Apache 2.0) and is in fact now cross-platform.
Miguel de Icaza of Xamarin showed it running on Mono at BUILD.
When Roslyn releases, it will become part of Mono. They are already maintaining a branch at the Mono Git repo.
As #Govert has already mentioned in a comment, if you want to embed C# scripting capabilities you should simply use the Mono-equivalent library/tool: Mono-Csharp. (Especially because, even if Roslyn could run on Mono, its licence may dictate that you're not allowed to.)
This tool in the Mono world has existed much earlier than Roslyn BTW, and is open source. Here you have even a Microsoft employee blogging about it and uploading it to Nuget:
http://blog.davidebbo.com/2012/02/quick-fun-with-monos-csharp-compiler-as.html
I hope your game will kick ass!

Does C# require .NET

I'm new to C# (but not to programming) and I was wondering: do C# programs always require .NET, or are there ways to avoid dependencies and make the application independent?
Yes C# always requires the .NET runtime.
If you are worried about other platforms there is Mono which will allow .NET applications to run on platforms other than Windows (i.e. Linux) using the Mono runtime.
C# code is compiled into CIL code which is a platform-independent instruction set, I quote from Wikipedia:
During compilation of .NET programming
languages, the source code is
translated into CIL code rather than
platform or processor-specific object
code. CIL is a CPU- and
platform-independent instruction set
that can be executed in any
environment supporting the Common
Language Infrastructure, such as the
.NET runtime on Windows, or the
cross-platform Mono runtime.
A CLI runtime/interpreter is required, but it doesn't have to be .NET.
Currently there is one other CLI interpreter, MONO, for Linux.
C# isn't compiled to native code, and therefore the computer can't read it. You need the .Net framework to convert the so called bytecode (the code that the C# compiler compiles to, CLI) can be converted by the Just In Time compiler of the .Net framework.
Mono is an alternative framework, and it can also run C#. It is supported on more platforms then then the .Net framework (that only supports Windows).
So yes, either the .Net framework or the Mono runtime is needed to run C# applications, new versions of Windows automatically install and update the .Net framework.
Apparently .NET runtime is not required, see MonoTouch for example.
It is possible. C# is just a programming language, and just like any other programming: It can be compiled in any way you can think of: That includes compiling without .NET. In fact, the MONO project (C# for a lot of platforms) does just that.
There are some other commercial compilers available that will pretend to compile your application without .NET , but they just stick .NET in your executable: which is useless, slow and just stupid.
Any other way to use C# without .NET or MONO would be more of an educational experience than a practical solution. As for what the education experience is worth: If you have the time, I would definitely recommend trying something like it when you have a bit more experience.
Just as C programs typically require a C runtime, C# programs require the common language runtime.

Using Mono for developing in C++

I am starting to use Mono to develop applications in C# and C++. I wanted to ask you, how is Mono compiling the C++ code? is it using GCC? It is amazing to see that it has the STL containers... Also, can I use the Boost libraries and GSL libraries with Mono? Thanks in advance!!!
I think you must be using MonoDevelop, the IDE, as opposed to Mono itself.
Yes, MonoDevelop uses gcc/g++ to compile C/C++ source code, but it is not compiled to CIL - it is compiled to a native binary.
If I am understanding correctly, then you should be able to use boost just fine.
If, however, you are asking if Mono has support for Mixed-Mode assemblies or executables (e.g. assemblies/exe's that contain both native and .NET CIL), then I am sorry to inform you that this feature is not supported, nor is compiling C++ to pure CIL by Mono.
As long as you don't need mixed mode (i.e., forget the native part and go for CIL-only), mono does work with C++ code (I hear they're now experimentally supporting mixed mode, on Windows especially, and elsewhere via wine, but I think that part's NOT ready for prime time). The one well-supported C++ compiler at this time is Microsoft C++/CLI on Net 2.x frameworks; efforts have been underway (for many years now) to add gcc, but I don't know of any production-ready result so far:-(.

How to run C# project under Linux

Do you know any ways to run a C# project under Linux. Are there any framewoks or libraries for this?
You're looking for the Mono Project - a cross-platform (but primarily targeted at Linux) implementation of the .NET Framework and CLR. It's capable of running binaries compiled for the CLR (MS .NET), or of creating its own native Linux binaries.
The project has been going a while now, and it's current version (2.4) is very usable, even for production purposes. See the project roadmap for details of the main features and milestones of current and future releases.
Details about the current state:
The great majority of the BCL (Base Class Library) is available on Mono, with the exception of some of the .NET 3.0/3.5 stuff, such as WPF (which has minimal support currently) and WCF (almost non-existent support). Silverlight 2.0 is however being supported via the Moonlight project, and progress on that is going well. WinForms functionality (which uses GTK# as a backend) is however quite complete, as far as I know.
Implementation of the C# 3.0 language is effectively complete, including the C# 3.0 features such as lambda expressions, LINQ, and automatic properties. I believe the C# compiler is mature to the point that its efficiency is at least comparable with that of the MS compiler, though not yet matching it in some respects. What's quite cool (and unique) about the Mono C# compiler is that is now offers a compiler service - in other words true dynamic compilation from code (without using the CodeDOM). This is something that MS will perhaps only add in .NET 5.0.
Like others have already said, you can run .NET applications on Mono. If your applications use Platform Invocation (P/Invoke) to call native code, you may run into some trouble if there is no Mono implementation of the native library. To check whether your application does that (or uses APIs that haven't been implemented in Mono yet), you can use the Mono Migration Analyzer (MoMA).
For those who come across this question post 2016, can use .NET Core - An open-source, general-purpose development platform maintained by Microsoft and the .NET community on GitHub. It's cross-platform (supporting Windows, macOS, and Linux) and can be used to build device, cloud, and IoT applications.

Cross platform programming on Windows

Two Questions:
Is there any way to write cross platform programs on Microsoft Visual Studio?
If there isn't then could I write a C# application on VS2008 and recompile it with MonoDevelop and have it work?
1 - I dont' think so. Not without something like Mono.
2 - Yes you can, but Mono doesn't cover all the framework - they are working on it.
The best thing to do is check with the Mono Migration Analyzer. The Mono Migration Analyzer (MoMA) tool helps you identify issues you may have on Mono - http://mono-project.com/MoMA.
I have found most of my .NET 2.0 applications can be converted, but you may need some tweaks.
You can always use C++ and QT. Soon QT will be released on LGPL license (from version 4.5) that will give some more freedom.
The only limit of using free QT license is that you don't get integration with VS. However this can be handled by using eg. CMake (which will generate VS solution files).
Yes, Write your code, compile and run on another platform using Mono. When you compile you generate IL, which Mono can use. Note: Some functions aren't available on Mono. Delphi Prism, is an add on for Visual Studio which allows you to code to Linux and Mac from VS albeit in the object Pascal language though.
Yes, see same issues as in 1
I agree with Joe90, just one thing he left out: MonoDevelop can compile .sln and .csproj files because it has a MSBuild implementation.
So point MoMA at your code and if you get a green light it should compile as-is in MonoDevelop.
As a MSCLR junkie I have to admit that Mono has a few 'better' implementations of certain critical functions (mostly to do with encryption). You will get more usability power from these.
Another thing to watch out for is subtle logic errors. If a class is implemented in Mono it does not mean that it will behave the same the MSCLR one (Mono is a cold-room implementation and as such they DO NOT use the original source code). This is really where you will get good results from a well unit-tested code base.
For a good indication of what you should expect, I remember seeing a large amount of #if MONO in the AgsXMPP repository.
You could ideally write a C# application and have it run on the Mono platform. BUT, that will depend on the libraries of MS .NET that has been ported yet to Mono.
Just in case, there is no language constraint, you could consider using other languages like Java, Python, Ruby and the like..
Good Luck!
As many others mention your success will depend on the libraries you use. Mono does have Winforms but I would suggest that you also look at GTK# http://www.mono-project.com/GtkSharp as your windowing library. If you use GTK# you will use a library which is not reverse engineered (as Winforms is in Mono).
My understanding is that "non-gui" .NET 2.0 stuff is pretty much in place with the newest versions of Mono
You don't need mono develop, the whole idea of mono is you can develop for .net and have the same assemblies work on both mono and .net, provided you only use stuff which has been implemented in mono.
Stuff which won't work:
pinvoke,
wpf,
linq to SQL
Stuff which will work:
.net 2.0,
c# 3.0 (including linq to objects and linq to xml),
winforms
If you write your application in Silverlight, anyone with a web browser and the Silverlight plugin can access your app. This is as cross-platform as you're going to get with .NET.

Categories