F# Library in Silverlight Application? - c#

I have an F# library in the same solution as a Silverlight (C#) project that I'd like to use the library in. When I try to add a reference to the library project, it says I can only add Silverlight references. How can I make this F# library compatible with my Silverlight project?
I'm using Visual Studio 2010.

You need to start with an 'F# Silverlight Library' rather than 'F# library' project template.

The F# compiler automatically generates assembly for some non-standard runtime (such as Silverlight or Compact Framework) if you reference the appropriate (non-standard) version of the mscorlib.dll and FSharp.Core.dll assemblies in your F# project. The Silverlight version of the assemblies can be found here (on my machine):
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\Silverlight\v3.0\mscorlib.dll
C:\Programs\Development\FSharp-1.9.9.9\Silverlight\2.0\bin\FSharp.Core.dll
This is a more user-friendly replacement for the --noframework and --cli-root parameters mentioned in the question referenced by Pretzel.

Related

Compiling .NET project with different references and frameworks

I am building an SDK dll that needs to support multiple .NET frameworks with different references.
For example, one SDK dll is for Azure Functions which is .Net 4.6.1 with Newtonsoft.Json reference 9.0.0.1 (which was hard coded into Azure Functions). The other is a .NET 4.5.1 framework with newtonsoft.json 10.0.3.
What is the best way to build 2 dll's from the same cs code files using one Visual Studio solution where frameworks and references are different?
thanks
You can use the new .Net Standard class libraries
https://learn.microsoft.com/en-us/dotnet/core/tutorials/library-with-visual-studio
With that you can target the minimum library in your case it will be 1.2
https://learn.microsoft.com/en-us/dotnet/standard/net-standard
Them you can reference this dll from both projects.
For the referenced assemblies you need NOT specify a specific version of your dependency. There is a detailed explanation on how to do that on this answer here.
How exactly does the "Specific Version" property of an assembly reference work in Visual Studio?
Using SharedProjects in visual studio 2015 or greater allows for the targeting of multiple frameworks and references.
http://www.c-sharpcorner.com/UploadFile/7ca517/shared-project-an-impressive-features-of-visual-studio-201/

Referencing .NET Standard csproj project from .NET Framework traditional project

I have a library written against .NET Standard 1.3 which should be compatible with the .NET Framework 4.6.1 runtime. The library uses the new csproj and works fine when referenced by .NET Core projects.
I also have a WPF project on a traditional csproj which should be able to use this library. In Visual Studio 2017 I managed to reference the .NET Standard library and it compiles without errors.
At runtime, it tries to load assemblies which are dependencies of the .NET Standard project and it fails with assembly not found exceptions. And I can see that my output folder doesn't have any of the dependencies indeed.
Is there a way to reference my library in such a way all the required dependencies are copied to the output folder (or an equivalent solution to make the WPF project run fine)?
Note: If I reference manually one by one all the missing dependencies and use runtime assembly version binding I can make it run, but I shouldn't really do this when referencing a project.
This is the full project page: https://github.com/UnoSD/PasswordManager
.NET Standard csproj
WPF package.config
WPF csproj
It currently is a Roslyn problem: GitHub issue
It is an active issue on Roslyn:
https://github.com/dotnet/roslyn/issues/17639
https://github.com/NuGet/Home/issues/4488
You have to specify compiler output for .NET 4.6.1
Put line like below into your csproj in your .NET Standard library project.
<TargetFrameworks>netstandard1.4;net461</TargetFrameworks>
(that should go instead <TargetFramework>netstandard1.4</TargetFramework>)
Build will produce binaries both for .NET 4.6.1 and .NET Core runtime compatible with NET Standard 1.4.

In Xamarin, Is it possible link to a .NET 4.0 assembly?

I don't have Xamarin yet, and i want to build some application both for android and windows, each will use same shared library.
Is there a possibility to link .NET 4.0 assembly to Xamarin ios/android project, without recompile?
This assembly reference to System only, uses SQLite-NET by praeclarum, which requires native Windows library sqlite3.dll.
As i heard, on Mono/Xamarin? Mono.Data.Sqlite is required instead sqlite3.dll, but i am not sure, can i keep my library "cross-platform" with it? Or i will have to change something?
Unfortunately, i cannot check this currently.
Thank you..
.net assemblies can't be linked to Xamarin Android/iOS but you can link PCLs or you can create a Xamarin Android/iOS project, link files from your .net assemblies there and resolve possible problems with conditional directives (#if...).

.NET 4.0 assembly built on windows 8.1 does not work on lower windows versions

We have a project consisting of c++ core library and .NET wrappers (2.0 and 4.0) using Marshall. Build machine has Windows 8.1 OS.
C++ core and .NET 2.0 wrapper are built using MSVC 2005 and works perfect on other machines with lower windows versions.
.NET 4.0 wrapper is built using Microsoft SDK 7.1. Library works fine on build machine, but crashes on other machines (with .NET 4.0 installed) with following error:
Exception: System.MissingMethodException: Method not found: 'IntPtr System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate(!!0)'.
It seems like wrapper was built using higher version of .NET and i have no idea how to solve this. As far as i understood there is no way to force using specific version of .NET 4.x while building though they are not backwards compatible.
You can specify the version of the .Net framework you want your app to use in the project properties window. Under the Application tab, select your preferred version under the Target framework dropdown. You can see more about targeting specific framework version on MSDN.
I managed to solve the problem. Somehow MSbuild used the best avilable toolset, though environment was configured to Windows SDK 7.1
While investigating the problem i finally found this article. So in order to build project i must configure environment to SDK and tell MSbuild to use toolset from this SDK.
So the solution is to call MSbuild with flag /p:PlatformToolset=Windows7.1SDK.
Thanks to everybody who was helping!
Because I don't use Visual Studio projects or MSBuild, I had to find out how to deal with this at the C# compiler's command line. It isn't that complicated, but there are some new concepts. The .NET assemblies in the same directory as csc.exe are "implementation assemblies". When you want to compile for a particular .NET version, you should use "reference assemblies", which are explained here: ILDasm, mscorlib and System.Runtime decompilation differences depending on the directory
You find reference assemblies under c:\program files (x86)\Reference Assemblies\Microsoft\Framework\.NET Framework. In there are directories for 3.5, 4.0, 4.5 and 4.5.1, on a machine with VS.2008 to VS.2013 installed. To make use of them, you need a command line like:
csc /target:library /noconfig /nostdlib+ /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NET Framework\v4.5\mscorlib.dll" MyLibSrc.cs
/noconfig tells the C# compiler to ignore csc.rsp, which provides it with a default list of assemblies to reference, which are the implementation assemblies you don't want.
/nostdlib+ tells the C# compiler not to use its default standard library.
/reference tells the C# compiler that it can use the library whose pathname follows. The one shown here is the standard library for .NET 4.5: the project I built this with only uses the standard library, so that was all I needed.

How to compile a .NET assembly against Silverlight?

I am trying to compile AvalonEdit for Silverlight, but I am not sure how to do it as I never worked with Silverlight, only WPF.
I tried to add this control to my Silverlight application but Visual Studio complained that the assembly wasn't compiled against Silverlight.
I thought it was gonna be a matter of picking Silverlight under the target platform in the project settings.
Any ideas?
Silverlight and WPF assemblies are not compatible. So any assembly you reference from your Silverlight assembly must also have been compiled as a Silverlight assembly. I'm assuming your reference to "project settings" is your assembly, not the third party assembly.
But, compiling WPF code as Silverlight may be challenging. Silverlight is a functional subset of WPF so the code may use features not available in Silverlight.
You need to create new project which targets silverlight (File - New Project - Silverlight) and then add all the source code from the AvalonEdit source code into that library.
Edit:
Also note that the control might use classes that are not available in Silverlight. From the codeplex discussion related to this control (from year 2009):
Silverlight does not have the System.Windows.Media.TextFormatting namespace, so you'd have to rewrite a large part of the ICSharpCode.AvalonEdit.Rendering namespace if you want to port AvalonEdit to Silverlight.
In silverlight projects you can't reference non-silverlight dll's or projects. So in this project if there is an essential non-silverlight dll for building this project. You cant do this build.
If there are just some usages wfp supports but silverlight doesnt support find out this differs.
Then exclude these codes or use pragma
#if !WPF
//Todo
#endif

Categories