0

Can you share .NET DLLs (written in VB.NET using Visual Studio) between Full.NET, WindowsPhone, Silverlight, MonoTouch, MonoDroid. (And in Future also WinRT)

Is this possible?

I'm not looking to share UI code mostly just Models but maybe also some ViewModels (i.e. Using MVVM)

I was wondering whether using a Portable DLL would work? See http://visualstudiogallery.msdn.microsoft.com/b0e0b5e9-e138-410b-ad10-00cb3caf4981/

More details: This is not something I need today. I am looking at re-architecting existing projects written in VB.NET for .NET 4. This is going to be over the next year or so. Just like to have an idea whether Mono* devices will be able to play...

Thanks, Mike G

MikeG
  • 1,069
  • 1
  • 9
  • 19
  • Monotouch compiles to Native ARM code and doesn't use the CLR so it can't use .NET DLLs. If can use the source code though... depending on what APIs you call. – AnthonyLambert Jan 27 '12 at 13:21
  • @AnthonyLambert the final, native code for device will be an ARM binary. All other steps (and simulator) use .NET assemblies. In fact even the final code (an devices) will get the .NET assemblies (but without IL) since reflection can be used on the metadata and this is supported by the runtime (CLR) that is linked inside every MonoTouch application. – poupou Jan 27 '12 at 13:38
  • Part of the the question is can .NET DLLs be used on MonoTouch. The answer is no - you need to recompile them. Unlike Mono on the Mac for example where I have used .NET DLLs compiled on Win without recompiling them. – AnthonyLambert Jan 27 '12 at 13:44
  • Thanks anothony that's the info I was looking for, shame you haven't done it as an "Answer" (I can't click accept on a comment!) – MikeG Jan 27 '12 at 16:43

3 Answers3

3

With regular .NET assemblies: a combination of yes and no. Broadly, yes. But in reality there are often platform-specific changes that are needed due to limitations on each platform, which will stop it running the assembly. The IDE also doesn't like certain combinations of references. The monodevelop IDE is IIRC more permissive in terms of what assemblies it will accept.

Re things that don't work; it can be silly things like whether Trace.WriteLine(...) exists, or (more recently on "unity", Interlocked), or it can be platform restrictions (you can't do as much meta-programming on iOS, for example).

WinRT sets a new "bar" for breaking things, with radical changes throughout (fundamentals such as Type have been hugely altered).

My advice: build per platform; otherwise you are limiting yourself to the least common denominator, which is not optional. Make use of what exists on each platform, with fallbacks for when it doesn't.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
2

You can share the source code across projects for different platforms. You create the file once and link to other projects targeting different platforms. See this.

You can't share the DLLs. You can't for example reference .NET "desktop" assembly in Silverlight project because of the selected target platform.

Community
  • 1
  • 1
Karel Frajták
  • 4,389
  • 1
  • 23
  • 34
  • Sharing code is an interesting Idea but may get complex as there will be several DLLs, and I forsee a headache managing dependacies betwen code files in respective projects of different types. Some of it will be company wide library code, some of it app specific code etc... But thanks for that link i'll investigate... – MikeG Jan 27 '12 at 09:41
  • Although not the answer I was after, this has been the most useful. Sharing code files will be really helpful in the next year while i'm slowly migrating code files to new MVVM architecture. Also is what will have to use for Mono* products. – MikeG Jan 30 '12 at 10:04
1

Yes, you can! (tm)

There are some limits though: You already mentioned UI related stuff, but you have to cast a wider net: Your DLL may not make use of and it may not reference anything, that is inaccessible in one of the platforms.

With a little time warp you can even include .NET CF

Eugen Rieck
  • 64,175
  • 10
  • 70
  • 92
  • Thanks.Do you mean yes you can use a portable DLL? – MikeG Jan 27 '12 at 09:25
  • This is not possible - for example you can't reference Silverlight DLLs in WPF app and vice versa. – Karel Frajták Jan 27 '12 at 09:28
  • Karel, What about portable DLLs? (See my link) – MikeG Jan 27 '12 at 09:29
  • @KarelFrajtak as I mentioned in the answer, you may not **reference** Silverlight, WPF or similars. A DLL you can compile with `$COMPILER /target:library mysource.xyz` will work as given, as long as the source doesn't **make use of** something not portable. – Eugen Rieck Jan 27 '12 at 09:31
  • @Eugen, even if you don't use UI specific stuff, you can't create general purpose DLL that uses for example client profile System.dll (i.e. Silverlight) and use it in WPF - you have to create platform specific DLLs. – Karel Frajták Jan 27 '12 at 09:40
  • Karel, please see http://visualstudiogallery.msdn.microsoft.com/b0e0b5e9-e138-410b-ad10-00cb3caf4981/ this is designed to do exacly what you say can't be done! – MikeG Jan 27 '12 at 09:44
  • @KarelFrajtak as I already mentioned above, it will quite easily. Google for ".net framework retargeting" - we use it all the time to deploy **exactly the same binary** to CF, Desktop and Mono. So - you might downvote your knowledge of .Net instead of my answer! – Eugen Rieck Jan 27 '12 at 09:46
  • Now I agree with you, you should have mentioned this in you answer. Please edit your answer so I can upvote it. And thank you for sharing this information, I didn't know it. – Karel Frajták Jan 27 '12 at 09:54