Netduino home hardware projects downloads community

Jump to content


Photo

Is this possible - Mixed Visual Studio Solution (.NET Mainstream and .NET Micro Framework apps, shared Class Library)


Best Answer dranuag, 27 August 2015 - 06:49 AM

Ian, 

 

Regarding the "Add as link" functionality of Visual Studio, here is the reference from MSDN : https://msdn.microso...(v=vs.105).aspx

 

Paul 

Go to the full post


  • Please log in to reply
4 replies to this topic

#1 TechnoGuy

TechnoGuy

    Advanced Member

  • Members
  • PipPipPip
  • 51 posts
  • LocationCanada

Posted 26 August 2015 - 02:37 AM

Hi I'm looking to do the following...

 

Create a Visual Studio solution consisting of 3 projects:

 

Project #1:  A simple Class Library consisting of a single class that does some fairly modest stuff.  It is specifically designed to make use of base classes that are COMMON to both the .NET (Mainstream) Framework and the .NET Micro Framework.

 

Project #2:  Implements a Windows Forms application, which consumes the class created in Project #1.

 

Project #3:  Implements a Netduino Universal application, which also consumes the class created in Project #1.

 

Is this even possible?  If so, could somebody offer some suggestions as to how to make it happen?

 

I find that I can Add References to the shared class from both my .NET (Mainstream) and .NET Micro Framework applications at design time & that everything resolves at that time.  However, when I try to build my project, I get errors to this effect:

  • Error Could not copy the file "C:\Users\Ian\Documents\Visual Studio 2015\Projects\Experiments\Mixed Solution NETMF and NET\MathStuff\bin\Debug\LE\MathStuff.pe" because it was not found. NETMF Calculator
  • Error Could not copy the file "C:\Users\Ian\Documents\Visual Studio 2015\Projects\Experiments\Mixed Solution NETMF and NET\MathStuff\bin\Debug\LE\MathStuff.pe" because it was not found. NETMF Calculator
  • Warning The project 'MyMathLib' cannot be referenced. The referenced project is targeted to a different framework family (.NETFramework) NETMF Calculator
This in retrospect makes sense to me because - when I created the shared class - I guess I did it as a .NET (Mainstream) Framework class library as opposed to a .NET Micro Framework class library.
 
I just want to create a single code base that can be consumed by programs on both the Mainstream & Micro side of things.
 
I have looked at the Visual Studio Multi-Targeting Overview; it sounds like you can target different versions of the .NET (Mainstream) Framework but not simultaneously.  Also, when you actually try to do some of the things described in that article, it only shows the mainstream framework versions as options (it does NOT show any of the Micro Frameworks (at least for me).
 
Visual Studio Multi-Targeting Overview
 
Maybe I should be looking at something else?

 

Lastly, to answer the question of WHY would I want to do this.  I have 2 reasons:

  1. First, I'm looking to speed up my process in terms of creating a class library that I can utilize on the Netduino.  I find that the Edit / Compile / Debug process (of deploying to the Netduino) is very slow compared to just staying 100% on the PC (during the development process).
  2. Secondly, with my application design, I'm actually looking to create a kind of an automated agent that runs on the Netduino (empowered to act on behalf of the user) AND an end-user application (with a full GUI) that runs on the PC which basically has the same capabilities (plus some additional features).

Attached is some sample code which illustrates the class consumption issue.  It's not my actual application.

 

Attached File  MathStuff.cs   1.45KB   2 downloads

Attached File  Program.cs   637bytes   4 downloads

Attached File  Form1.cs   1.31KB   1 downloads

 

I will certainly appreciate any suggestions that you may have.

 

Thank you,

Ian


- Ian

 

My Current Dev Boards:

  • 3 x Netduino Plus 2
  • 1 x Netduino 3 WiFi

#2 dranuag

dranuag

    New Member

  • Members
  • Pip
  • 9 posts
  • LocationLuxembourg

Posted 26 August 2015 - 10:11 AM

Hi Ian, 
 
Depending on the situation, there are several ways to "share code" between .NET, NETMF, Silverlight, WindowPhone, Windows 8, Universal App & Mono projects. 
 
The obvious one, that does not apply for NETMF, is to define your Types in a "Portable Class Library" project, specifying the targets you want to address. 
 
The not-so-obvious one, is to define your Types in a "Class Library" project, and then "add as link" the corresponding files in the projects where you want to use them, assuming that the namespaces referenced by the code that exists on those projects/targets. If not, you can, under some conditions, use precompilation directives to adapt the code for each target. 
 
I use those techniques in a solution hosted on Codeplex, that can be downloaded for reference. In that solution, I want to share a Message entity, defined in the Models project, between .NET, NETMF and Windows Phone 8.1 projects. 
 
Thus, I have : 
  • defined the corresponding Types in the Models project, that will compile a .NET 4.5 library; 
  • added "as link" those files in the "PortableModels" project, a "Portable Class Library" project targeting WindowsPhone 8.1, that will compile a portable library supported by WP 8.1; 
  • added "as link" those files in the MFClient project, a "NETMF Class Library" project. 
 
Since NETMF 4.2/4.3 does not support enums, that I use in the Message class, I have used precompilation directives to remplace them by int for NETMF projects:  
 
#if !MF_FRAMEWORK_VERSION_V4_2 && !MF_FRAMEWORK_VERSION_V4_3
        [DataMember]
        public ContentTypes ContentType { get; set; }
#else
        public int ContentType { get; set; }
#endif
 
I use the same technique to share the code of a MessageClient class, defined in a "NETMF Class Library" project ,between two NETMF projects that will compile that class (and others) targeting NETMF42 & NETMF43. Check the projects MFClientShared, MFClient & MFClient43 from the solution.
 
Hope it helps,
 
Paul 


#3 TechnoGuy

TechnoGuy

    Advanced Member

  • Members
  • PipPipPip
  • 51 posts
  • LocationCanada

Posted 27 August 2015 - 05:40 AM

Paul - thanks so much for your comprehensive reply to my query.  It's right on the money with regard to what I am looking to do.

I am still looking at the fantastic solution that you have posted up on CodePlex / still taking it all in...  Nice video demonstration also, by the way...  :)

 

So anyway I have found the Core.PortableModels.csproj file (in the PortableModels project) and I do see the links that you talk about:

 

https://monitorandco...leModels.csproj

<ItemGroup>
<Compile Include="..\Models\Constants.cs">
<Link>Constants.cs</Link>
</Compile>
<Compile Include="..\Models\ContentTypes.cs">
<Link>ContentTypes.cs</Link>
</Compile>
:
:
:
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>

My question for you now is whether you were able to make those linking specifications through the Visual Studio IDE or if you had to go and edit the .csproj file by hand?  I've been looking through all the Project-level property settings and have yet to identify the place where you could set it up.  Tomorrow I will download your whole solution / load it into Visual Studio & see if I can figure it out that way...

 

Thanks again for such a comprehensive reply!

 

Ian


- Ian

 

My Current Dev Boards:

  • 3 x Netduino Plus 2
  • 1 x Netduino 3 WiFi

#4 dranuag

dranuag

    New Member

  • Members
  • Pip
  • 9 posts
  • LocationLuxembourg

Posted 27 August 2015 - 06:49 AM   Best Answer

Ian, 

 

Regarding the "Add as link" functionality of Visual Studio, here is the reference from MSDN : https://msdn.microso...(v=vs.105).aspx

 

Paul 



#5 TechnoGuy

TechnoGuy

    Advanced Member

  • Members
  • PipPipPip
  • 51 posts
  • LocationCanada

Posted 01 September 2015 - 05:42 AM

Paul,

 

Thanks again, this time for pointing me in the right direction (with the referenced MSDN article "Share code with Add as Link").

 

I was having a bit of a mental block in finding the Add as Link button in the Add Existing Item dialogue.  I finally noticed that the Add button in that dialogue was implemented as a drop down list; clicking the drop down arrow reveals the elusive (for me) Add As Link alternative.

 

I am happy to say that I have now been successful in creating 2 programs (one a Windows Forms application, the other being a Netduino application) which access the same piece of shared code.  I am really happy with Visual Studio's treatment of that code (I'm using VS2015 now) in that it (through Intellisense) gives clear indications about constructs which are NOT portable across the two framework versions.

 

For others who might be reading this post, I would like to point out that this technique accomplishes code sharing on a "per file" basis (i.e. a single <filename>.cs file at a time).  It is of course possible to share multiple .cs code files using the technique.

 

My original conceptualization of what I was looking to accomplish was to create a project consisting of a single class library (perhaps consisting of one or more classes, and possibly strewn over more than one .cs code file) and share the project between a Netduino application (.NET Micro Framework) AND a Windows application (full .NET Framework).

 

I'd just like to report that - from my sleuthing around - this too is possible, although (currently) not as easy to accomplish.  Check this very recent posting, made by user mcalsyn, over on the GHI Electronics forums.

 

Topic:  Using Shared Projects with NETMF

Posted By:  "mcalsyn"

Date:  August 27, 2015

Link:  https://www.ghielect.../topic?id=20173

 

With his technique, you have to temporarily unload one of your projects, go in & manually edit the projitems file associated with that project, and then reload the project.  I have tried his technique and can say that it does work as well.

 

I'm totally jazzed at the possibilities now...

 

Anyway, thanks again to you Paul (and also to mcalsyn on the GHI site)

Attached Files


- Ian

 

My Current Dev Boards:

  • 3 x Netduino Plus 2
  • 1 x Netduino 3 WiFi




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.