voidvector
voidvector

Reputation: 1996

Targeting individual platforms with Visual Studio

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.

Upvotes: 0

Views: 367

Answers (2)

Lex Li
Lex Li

Reputation: 63289

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.

Upvotes: 1

Brian Lyttle
Brian Lyttle

Reputation: 14579

#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.

Upvotes: 2

Related Questions