Specify the .Net Framework with IKVM - c#

I compiled a Java application to a C# dll with IKVM. I tried to use this dll in a C# project which uses .Net Framework 2.0 but failed since the converted dlls framework obviously is higher. I looked through the arguments of the IKVM compiler but didn't find anything regarding the framework.
My question now is:
Am I able to specify the framework version of the compiled DLL/EXE with IKVM? If so, how?
Thanks in advance.

The current version of IKVM 7.1 is build with the framework 2.0. If you create a dll with IKVMC then it use by default the framework 2.0. Another thing is if you build IKVM self.
If you want compile a dll for a higher framework as 2.0 then you need to use the command line parameter:
-nostdlib
And you need to set the all needed references (like mscorlib.dll) to the target framework with the command line parameter:
-r:<file name>

According to my search through all the IKVM versions the IKVM 0.38.0.2 version and later all contain IKVM binaries built with framework 3.5.
0.36.0.11 was the last version depending on 2.0. That version only supports Java 6 and is way too old anyway.

Related

Dafny 3.0 C# dll library problem from 3.0 to 3.2

The problem I have is the following: we have a series of functions, methods and datatypes in Dafny and with it we generate a c# dll library. In order to make use of those libraries, we have added them as a reference to a visual studio c# console application project.
The problem I have is that these libraries refer to a dependency to System.Private.CoreLib. The only place I have found this library is in the source code of dafny. By adding it as a reference, it becomes "incompatible" with all System.* libraries imported by default in c#. This makes it unable to create a class, method etc. Attached are screenshots of the problems I just mentioned.
Finally, if I compile these libraries with Dafny 2.0 (deprecated), I don't have this problem, but I would like to work with the latest version of dafny.
Thank you in advanced.
System.Private.CoreLib is part of the .NET Core runtime and doesn't have to be referenced directly. That's where the basic built-in types like Array, DateTime, Thread etc live. It's used implicitly by all .NET Core projects and doesn't have to be referenced explicitly.
The Dafny 3.0 release notes explain that the tooling migrated to .NET (Core) 5 from Mono and the .NET 5 version of Coco/R is used now
Tool
Migrate to .NET 5.0.
If you used to use mono Dafny.exe to run Dafny before, use dotnet Dafny.dll now.
Implementation
Use .NET 5.0 version of Coco/R.
Your projects will have to target .NET (Core) 5 as well.

Nuget Package compatible with C# and VB.NET

We have a legacy VB.NET application. The frontend is being redesigned in MVC 5, and I was assigned to abstract the business logic to an API.
I created a library with some code to make calls to the API (so the developer building the MVC 5 app can easily install/update the package and use it).
But I would like to help the developer responsible to redesign the VB.NET project to consume the API with the same NuGet package.
Is it possible to create one NuGet package compatible with both, or do I really need to make two packages?
I read something about .NET Standard, but I couldn't find anything about VB.NET, it looks they work for C# only (I'm not sure).
If it's possible, should the VB.NET project use the same .net framework version as the package in order to work?
A compiled .Net DLL is language agnostic: you can use a DLL written in any .Net language from any .Net language.
Note that in general you can only reference .Net DLL (.Net assembly) that is compiled for that or lower version of .Net compared to one you set target .Net version of code you compile. Ideally both DLL (on in NuGet and VB.Net) target the same .Net version, otherwise read MSDN: Version Compatibility in the .NET Framework.
The language does not matter for .Net Nuget packages because they all compile to the framework(s) they target. This means C# assemblies compiled from C# code can be decompiled into C# or VB .Net regardless of the original language they were created in.
A little more detail:
Nuget packages are limited by the framework version they target, so your legacy app should preferably target the latest version of the .Net framework (4.7.1 as of this post) if you want to maximally utilise third party libraries and target the latest .net standard (2.0 as of writing).
.Net Standard is a compatibility standard, which means that if a language runtime version supports that version of the standard, it is guaranteed to have certain APIs available.
https://learn.microsoft.com/en-us/dotnet/standard/net-standard
Based on the table in that documentation, if your legacy application is targeting .net framework 4.5, you can only build libraries that target .net standard 1.1. Any higher version for your nuget package will prevent you from using it in your legacy app.

Dynamic Link Library, Solution can't load assembly

Recently I started learning how to create Dynamic Link Libraries in Visual Studio with C#.
I followed online instructions on how to create DLLs:
https://learn.microsoft.com/en-us/dotnet/core/tutorials/library-with-visual-studio
http://www.c-sharpcorner.com/UploadFile/1e050f/creating-and-using-dll-class-library-in-C-Sharp/
https://blogs.msdn.microsoft.com/benjaminperkins/2017/04/13/how-to-make-a-simple-dll-as-an-assembly-reference-just-for-fun/
After completing these steps, I tried to add my .dll file to my projects.
On execution I receive this message:
System.IO.FileNotFoundException: 'Could not load file or assembly
'MySql.Connect, Version=1.0.0.2, Culture=neutral, PublicKeyToken=null'. The
system cannot find the file specified.'
I selected .NET Framework 4.6.1 when I started my project. The target framework in the Properties file under the Application tab is selected as .NET Framework 2.0.
The project that should reference to the file is targeting the .Net Core 2 Framework.
I've been at this problem for nearly a week and searched for online solutions.
Any help will be greatly appreciated.
You need to put the DLL somewhere that the runtime can find it. The easiest thing to do would be to put it in the same directory as the .exe, but you have other options as well (see How the Runtime Locates Assemblies).
It is important to carefully decide what platform you intend to use your DLL with.
I selected.NET Framework 4.6.1 when I started my project.
If you mean when you started your DLL project, then you have limited the types of applications that can consume this DLL to .NET Framework 4.6.1+.
Properties file under the Application tab is selected as .Net Framework 2.0.
If I understand correctly, you changed the DLL target from .NET Framework 4.6.1 to .NET Framework 2.0. This widens the compatibility so that the consuming library can be .NET Framework 2.0+. However, this is at the expense of all of the newer features of the .NET Framework.
Do note that official support (i.e. patches) for .NET Framework 2.0 has been gone for several years, and newer machines aren't likely to have it installed.
The project that should reference to the file is targeting the .Net Core 2 Framework.
Here's the crux of your issue. .NET Core ain't .NET Framework. It is a completely different platform.
That said, .NET Core has some limited support for referencing .NET Framework assemblies, but is it sure not to work with .NET Framework 2.0 (which again, hasn't been supported in years). Also, this "compatibility mode" probably means you lose cross-OS support, which is one of the main benefits of .NET Core.
Option 1
So, the knee jerk answer is to make your DLL target .NET Core if you want to use it with .NET Core applications.
Option 2
However, there is also an option to make a portable DLL that works with .NET Framework 4.5+ and .NET Core - make your DLL target .NET Standard.
See How to port from .net framework to .net standard for instructions on changing your DLL to target .NET Standard.

How can I correctly find the .NET runtime against which an assembly has been built?

I am trying to find the .NET runtime version of an assembly. I have looked at the two top answers of the: Determine framework (CLR) version of assembly but they do not seem to report the correct version.
I am building the assembly on a machine running .NET 4.6.1 and in Visual Studio the project is targeting .NET 4.5.1 however when I look at the assembly using the answers on the other question I get:
v4.0.30319
Instead of something along the lines of
v4.5.1.*****
What is going on?
Take a look at official Microsoft blog instead:
https://blogs.msdn.microsoft.com/rodneyviana/2014/12/23/identifying-the-net-version-you-are-running-2-0-4-5-4-5-1-or-4-5-2/
To make things more challenging, .NET 4.5, 4.5.1 and .NET 4.5.2 share the same version number as .NET 4.0 which is 4.0.30319. So, how can you tell which version you are running? You can use the same methods you used for .NET 2.0, except that the file of interest is rather clr.dll normally at C:\Windows\Microsoft.NET\Framework\v4.0.30319.
Apparently, the only way how to tell the difference is by looking at build number of clr.dll.

How to add .Net3.5 dll into .Net2.0 project?

I have a dll which is based on .net 3.5 -- it uses internally for example Linq, but the exposed API is straightforward, no fancy stuff. Since C# generics are resolved at compile time I assume that for calling party all it counts is API (all public parts).
However when I try to use this dll from net2.0 project I get info, that the dll cannot be referenced because the dll or one of its dependencies requires a later version of .net framework.
I can install any .net version I want on target computer (when entire app is installed), but I cannot change .net version for the project itself.
So: how to solve this? When adding a C dll to this project I had no such problems, so are C# dlls self-contained or not?
C# dlls need to have the .Net runtime to run as they are not compiled down to machine code. In this case the dll says it requires Net 3.5 so all your project will have to use 3.5 or higher.
To keep your project as Net 2.0 you would need to build another executable to contain the 3.5 DLL and communicate across separate processes.
The C DLL worked as it is compiled down to native code and does not require the .Net framework. (or at least not version higher than 2.0)
I've been using System.Core and the new System.Web.Extensions (for example) from 3.5 in an ASP.NET 2.0 app (using VS2005) for a while now with no problems. Similar to what Scott Hanselman blogged about here. So yes, it's possible.
.NET 3.5 still runs on the same CLR as .NET 2.0. So at runtime it's all the same. (Assuming you've tracked down any dependencies and copied those 3.5 DLLs to your bin folder as well.)
The only real limitation is what C# language features you can use at development time. Such as 'var', extension methods, or LINQ query syntax.
If you are using linq to objects, then you can use Linq Bridge:
http://www.albahari.com/nutshell/linqbridge.aspx
this is a Linq to objects implementation for .net 2.0.
You will still have to compile using vs2008 but you can compile with .net 2.0 as a target platform in that case.
(This is because the C# 3 compiler understands linq clauses even if you target .net 2.0, it will simply resolve the calls to linqbridge instead of the .NET 3.5 libraries in this case)
If you're using .NET 3.5 libraries then your application's requirements should be such that any consumer of it's API's should also be using .NET 3.5.
The only way you can bypass this is if you package all the dependencies of your application along with it. This means libraries your application uses which depend on the .NET 3.0 and 3.5 frameworks.
However, I'm not sure of the legality of ripping out chunks of the .NET frameworks and packaging them with an app. I'd read the EULA before doing anything like this. IMO, it's not worth the hassle; just install 3.5, ask your users to install 3.5 and be done with it or use only 2.0 features and libraries. At the very least, hacking around like this will only cause you more pain with deployment if there are framework updates in the future.
In either case, your app will work on .NET 2.0 as 3.0 and 3.5 are just extra libraries on top of the 2.0 runtime and libraries (as Craig mentioned) as long as all your dependencies are there.
C# DLLs are not self-contained. If your 3.5 DLL needs LINQ, it depends on system assemblies from the 3.5 (3.0 to be exact) framework, therefore the entire application depends on this version.
You could load the 3.5 assembly dynamically and use reflection to get access to the functions you need. This requires some overhead, of course.
Nothing pretty but there are ways to get the code happily working together (in the order of preference):
1) Upgrade both projects to 3.5
If I understand you correctly then your .net FW 2.0 Program will have dependency on 3.5 Library, which means for every functionality of the Program to work, it now requires FW 3.5. Since you state to have the code and authority to recompile the the Program AND install whatever FW on deployment, then you can upgrade it to 3.5. Sounds simple, but since you did not do this, then I guess you have good reasons (like other programs being higher up the call chain which you cannot upgrade to 3.5/recompile.)
2) Go around the FW2.0 compiler
Build the Program when referencing the 2.0 version of Library (or dummy, just providing the public API).
Build the 3.5 version of Library separately without Program (hence removing the need to reference the wrong FW assembly) and deploy the 3.5 version instead of the 2.0 version.
Since 2.0 and 3.5 use the same CLR runtime then fooling the compiler is enough. As long as the deployment maching has FW 3.5 installed, everything should be fine.
Note: everything is fine even if you have just .net 2.0 present on deployment machine and the user does not call .net 3.5 classes. If he does, there will be crash ;)
3) downgrade Library to 2.0
if you use only some classes of the .net FW then you could remain using the 2.0 compiler by adding those missing future assemblies to project. (this is the solution from Hanselman link shared by Craig). As already noted, you'll lose 3.5 compiler's syntactic sugar like vars.
Choose whichever suits your situation best.

Categories