0

I am writing an app that would work for both .NET and Mono. However components of it would only be available for one or the other. For example, a modular part of the app use WPF, which is not available in Mono (it would not even build!!!). Another part of the app uses PInvoke on a libpango, which is normally only available on Unix.

Is it possible to target individual platform with Visual Studio with different build parameter for each?

Currently The modular parts are not in their own project file, but I can easily move them to.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
voidvector
  • 1,976
  • 2
  • 18
  • 19
  • use #ifdef / #endif blocks to check for mono environment and put appropriate code in there or wrap your .NET and Mono stuff into generic calls and use the #ifdefs there, I forget what the define for the MONO environment is (probably MONO :P) – Jesus Ramos Feb 29 '12 at 03:05
  • possible duplicate of [How can I conditionally compile my C# for Mono vs. Microsoft .NET?](http://stackoverflow.com/questions/329043/how-can-i-conditionally-compile-my-c-sharp-for-mono-vs-microsoft-net) – skolima Feb 29 '12 at 06:48
  • I don't think it's an exact duplicate because this question is looking more at the dependency management (references) side of things. – Brian Lyttle Feb 29 '12 at 13:06

2 Answers2

2

#define/#if blocks are one method for doing this. There is also a conditional attribute. You might also want to look into MSBuild targets.

You might also want to think about your class design and whether you can share have shared interfaces that enable more of a provider or "plug-in" model to support different platforms. Microsoft developed a Portable Libraries project type that might help with this.

Brian Lyttle
  • 14,558
  • 15
  • 68
  • 104
  • I am already using #define/#if blocks. However, project references do not work. For example, a project with "PresentationCore" would still fail under Mono even if all such code is commented out. – voidvector Feb 29 '12 at 03:55
  • When building on the command line you can specify a list of references. I think that is where having multiple msbuild files would come in. It gets complicated but you would have one msbuild file that runs with VS (your main csproj file) and then another for mono. Both can be built on the command line - or include multiple targets in one file. – Brian Lyttle Feb 29 '12 at 03:59
1

Conditional compilation can make your code hard to understand and maintain. Personally, I am against it.

My suggestion is that you utilize good design patterns to isolate the platform specific bits,

http://codebetter.com/patricksmacchia/2011/11/07/real-world-feedback-on-a-net-to-mono-migration/

JavaDepend is a Windows Forms project, so its porting experience may help you.

WinForms executable (.exe) -> Platform adapter (.dll) -> Windows (.Windows.dll)
                                                     |
                                                     --> Other platforms (.Unix.dll)

In this way, you can always compile the whole solution together, package them together. At runtime, the adapter can load correct platform assembly/assemblies after platform detection.

For you case maybe you can use WPF for Windows, then you have to isolate WPF bits from business logic code, and then write UI again for other platforms (GTK# for Linux, MonoMac for OS X). This approach can be visualized as

WinForms/WPF executable (.exe) -> Platform independent biz-logic code (.dll)
                                  ^  ^
MonoMac executable             ---|  |
                                     |
GTK\# executable               ------|

In this way, you need to package differently for each platforms, but the core assembly/assemblies can be the same.

Lex Li
  • 60,503
  • 9
  • 116
  • 147