C# Do Roslyn work on Android (with Xamarin)? - c#

I recently use Roslyn to compile and execute code at the runtime of a game application. Thank to some useful ressource such as this web site and Vendettamit answer, I manage to code a program on a C# Console Net Core project on window 10 which execute this code located in a txt file at the root of the application.
using System;
namespace Test15
{
public class Program
{
public static int Main()
{
System.Console.WriteLine("Hello World from external Dll !");
System.Console.WriteLine("And it work !");
return 10;
}
}
}
I'm not going to share code because it is really similar to Vendettamit answer, and this program work well on a C# Console Net Core project on window 10.
So next step, I try to make this program work with the C# Monogame Framework on a Android project which use Xamarin.
First problem : when trying to add the nugget package "Microsoft.CodeAnalysis" which seem necessary for Roslyn, I have the 2 following error :
Unable to resolve reference 'Humanizer', referenced by `Microsoft.CodeAnalysis.CSharp.Workspaces`.
Add NuGet package or assembly reference for 'Humanizer', or remove the reference to
'Microsoft.CodeAnalysis.CSharp.Workspaces'. Game2
Unable to resolve reference 'SQLitePCLRaw.core', referenced by `Microsoft.CodeAnalysis.Workspaces`.
Add NuGet package or assembly reference for 'SQLitePCLRaw.core', or remove the reference to
'Microsoft.CodeAnalysis.Workspaces'. Game2
(Translated from French)
When replacing the "Microsoft.CodeAnalysis" nugget package by "microsoft.CodeAnalysis.CSharp", both errors disappears.
However at the runtime, adding the MetadataReference don't work.
For instance in the Console project I use :
MetadataReference.CreateFromFile(typeof(object).GetTypeInfo().Assembly.Location);
To add a MetadataReference to mscorlib.dll, but on an Android project, it crash because typeof(object).GetTypeInfo().Assembly.Location return "mscorlib.dll" instead of somethings like "C:\Program Files\dotnet\shared\Microsoft.NETCore.App\3.1.9\System.Private.CoreLib.dll", because we are on Android I guess. (same with other MetadataReference )
So when calling the method CSharpCompilation.Create(), the "reference" argument is an array of null MetadataReference and it crash.
Based on what I read, I think (but I'm not sure) that Roslyn can't work on android because of the missing location of System.Object.
So can someone can confirm (or invalidate) if there is a way to work with Roslyn on Android ?
(Bonus point if you know some other way to compile/execute or "interpret" C# code on any device)
Thank for reading and stay safe.

Related

Wrong reference of Newtonsoft assembly for class library in another program

I have a class library in .NET Framework 4.7.2 and I use a reference to Newtonsoft.Json Version 12... something.
If I run the app as a windows application (there is a winform) everything works just fine. However, it is actually a class library that is being called by another program as a plugin. Inside that program I get however this error
Which translates basically to "couldn't find the Assembly...". There Version=12.0.0.0 is begin referenced and the dll in that particular folder (the plugin folder) says version 12.0.3.23909
I tried to clean the solution, delete every reference to newtonsoft I found etc. but the result is always identical. What am I missing?
Sorry if this question has been answered before, I searched dozens of previous questions, but I can't seem to understand what is going on appart from "redo everything", which doesn't work here.
UPDATE
The output bin is as follows
If I change the program to a windows application and run it by calling the exe, it works, but not via the other program where it should run as a plugin. Interestingly, the core dll is being opened and the baseform works, but not newtonsoft.
Change at least one of the versions. I mean:
Change the version of Newtonsoft of your class library to -> 12.0.3.23909 and then build the class library and use it as a plugin.
Or change the Newtonsoft version of your project to 12.0.0 and then use your class lib as a plugin inside it.
Or:
You can carry Newtonsoft (12.0.0) with your class library and load both of Newtonsft and your class library as a plugin. (this works too but you should be aware of Dll conflict between 12.0.0 and 12.0.3)

Roslyn throws The language 'C#' is not supported

I have created a class library project and did some processing and also used Roslyn to generate code.
I use the library in a WPF GUI application as a reference.
These are the NuGet packages:
Build shows no error, however when I use the following code:
private static void GetGenerator()
{
workspace = new AdhocWorkspace();
generator = SyntaxGenerator.GetGenerator(workspace, LanguageNames.CSharp);
}
I get an exception:
"The language 'C#' is not supported."
at: Microsoft.CodeAnalysis.Host.HostWorkspaceServices.GetLanguageServices(String languageName)
at: Microsoft.CodeAnalysis.Host.Mef.MefWorkspaceServices.GetLanguageServices(String languageName)
at: Microsoft.CodeAnalysis.Editing.SyntaxGenerator.GetGenerator(Workspace workspace, String language)
According to this and this, I have to copy the CodeAnalysis files locally and add the necessary references. They are there, yet the error occurs.
Is this still a bug that wasn't fixed in the last year?
What else should I do?
Most likely it's because you don't reference Microsoft.CodeAnalysis.CSharp.Workspaces in your code, i.e. you never use a type or method in this dll, so MSBuild thinks it's not needed (see e.g. this question).
So what you could do is e.g. add the following line somewhere in your class library project:
var _ = typeof(Microsoft.CodeAnalysis.CSharp.Formatting.CSharpFormattingOptions);
Then MSBuild should copy Microsoft.CodeAnalysis.CSharp.Workspaces.dll over and everything should be fine. No need to reference the NuGet packages from all the other projects.
You have to add the Microsoft.CodeAnalysis package to both the class library project AND the referencing project as well.

Drag and dropped DLL throws MissingMethodException

We have an app A that has a reference to an assembly B that contains some static methods. Assembly B is in the same Visual Studio solution as app A.
We want the users of our app A to be able to write plugins. They build the plugin (at present) by creating a new Class Library solution in Visual Studio, getting the Nuget Package for app A, and adding a reference in their plugin solution to assembly B, so that the plugin code compiles.
They do not need to test their plugin, which is why the code just needs to compile, although it would be nice if they could debug their code when running it in app A.
Once their code has compiled, the DLL for the plugin is put in a share and given to a different team (bureaucracy I know) who put it with the rest of app A's DLLs.
I would like the plugin DLL to use the assembly B DLL that is with all of app A's DLLs.
When I run app A, the Activator class picks up the plugin DLL and correctly creates an instance, but as soon as one of the static methods from assembly B is called, the plugin throws a MissingMethodException.
Things I have tried:
The plugin solution definitely works fine if you create it and compile it within application A's Visual Studio solution.
App A uses framework .Net 4.5, assembly B uses framework .Net 4.0, I have tried building the plugin with both frameworks without success.
The "Specific Version" of the reference to assembly B in the plugin solution was false in all cases tested.
I would welcome immediate solutions to this problem but also broader architectural suggestions on how to get these plugins to work. I apologise if there is a duplicate question of this, I couldn't find one.
For starters you can use "Dotpeek" to decompile dll and see if the method defination exactly matches.
It's a free software available to decompile dotnet libraries.
If you don't have access to .pdb file then i would recommend using "dotnet reflector",or "IL Spy" it will decompile without pdb files.
Also do make sure you are referencing project in visual studio not the output dll.
I managed to fix my problem as follows:
Although the plugin only directly used static methods in Assembly B, these static methods actually made a chain of calls to various OTHER assemblies.
Instead of just adding a reference to Assembly B in my plugin, I did a Nuget command:
Install-Package -Id AppA -ProjectName Plugin
And this downloaded the latest AppA to the packages folder and added a reference to EVERY dll of AppA.
Like before, it compiled, but this time when I dragged the plugin dll into the AppA bin folder, the plugin code ran without throwing an exception.

How to run C# class library which is added to VB.NET existing solution?

Hi I have console application which is written in VB.NET. For this I added c# class library with existing solution. I wrote code in c# class library. Now when I try to run, it is going to VB.NET console application by default. I am not able to run my c# class library.
For clear understanding lets call VB.NET project as classVB and class library as classC#
I tired these methods to make it work :
1) Added classC# reference to my classVB project and made classVB as startup project. I used using statement also to refer to my classVB project as Using classVB. And I put break point in my classC#.But still it is pointing to classVB project
2) Tried to made classC# as startup project( Even I knew this doesn't gonna work). For this I am getting usual error which says "A project with an output Type of class library cannot be started directly."
3) Right click on solution and start up project option -> Single startup project and selected classVB.
But none of this is working. It is always pointing to my classVB project even after I put breakpoint on c#class library code.
Its the first time I am working on library class, so any help on this would be appreciated.
You can't directly run a class library. It is not executable. You can reference the code from it in your VB project, but a class library can never run by itself. To access a public method in your class library from VB, refer to it by Namespace.ClassName.MethodName.
Change the compilation output of the library to the same path where you added the reference in the VB.NET application. Generate the library and check the build is successful.
Do not forget to change the library class to configuration debug in the solution to generate the PDB file that will allow you debugging. Put a breakpoint and try again.

Create Project Dependency and Add Reference to Project still causes "The type or namespace name could not be found"

I have a solution in Visual Studio 2010 containing 6 projects (1 web application, 4 c# class libraries, 1 c# console application).
The console application is my test harness and use this to test external web services, output from methods from within my other libraries and general experimentation. This test console application has only one dependency on another project dependency, one of the C# libraries.
The referenced C# library is pretty simple:
namespace GowallaAPI
{
public class Gowalla
{
private static readonly ILog log = LogManager.GetLogger(typeof(Gowalla));
public SpotsInRadius GetGowallaSpotsInRadius(decimal lat, decimal lon, int radius) {
//snip
}
//other methods removed for brevity//
}
}
I have added to my console application a project reference:
And I've also right-clicked on References and selected Add Reference...
Then, I've gone to my console application and added;
using Gowalla;
Then hit build. I get this:
The type or namespace name 'Gowalla'
could not be found (are you missing a
using directive or an assembly
reference?)
I am completely baffled. I have:
Remove the dependencies completely (and then rebuilt with Gowalla references removed), and added them again.
I have removed the dependencies completely (like #1) and then added them as assemblies only (Add Reference...).
Checked that the target framework for both console application and class library is .NET 4.0 - they are.
Checked that all necessary items within the Gowalla class library are marked as Compile in the Build property.
Jiggled the build order of the project so that I am at least building the console application AFTER the library is built.
Done some shouting and swearing.
Given up and then returned.
Moved the Gowalla C# library out to its own project entirely and then referenced the assembly (like in 2).
Playing the having a constructor in Gowalla and not:
public Gowalla()
{
}
... and nothing has worked!
Can anyone see something obvious? Am I being utterly stupid? I have been on this for hours and I wonder quietly if this is a classic 'wood for the trees' moment...
Help appreciated.
EDIT 1: This is the Gowalla.dll exposed from Reflector:
ANSWER: After #gov's helpful suggestion to remove the GowallaAPI library and try and add something else I did that and started adding in the old code from the GowallaAPI library. Everything worked until I added:
private static readonly ILog log = LogManager.GetLogger(typeof(Gowalla));
log4net for some utterly bizarre reason kept throwing the build. Alas, after removing the line (the reference to log4net remains), the project built and worked perfectly thereafter. Thank you to #gov for setting me on the right path! :D
I had the exact same problem with log4net and it was resolved after changing target framework of the hosting project from ".NET Framework 4.0 Client Profile" to ".NET Framework 4.0"
I suggested him various things in the comments looks like one of them worked out.
#dooburt just forget about GowallaAPI and create a separate project like i say , sample.common and have a public class called utilities or so add that project here , just check a new project of type library and see whats the problem
Take a look at the .csproj XML, see if there is anything odd about the reference, one of these:
<Reference Include="Gowalla" ... />
<ProjectReference Include=".\path to\Gowalla.csproj" ... />
Have a look at the target framework of your class library and the test harness. I was having this error when the class library was set to .Net Framework 4 and the test harness was .Net Framework 4 Client Profile.

Categories

Resources