i have seen in the link below:
Can execute Code Dynamically in monotouch?
that it is impossible to use dynamic in ios xamarin. how about in Andoid xamarin?
I tried to do the following:
dynamic MyDynamic = new System.Dynamic.ExpandoObject();
MyDynamic.A = "A";
MyDynamic.B = "B";
However, when I want to access MyDinamic.A, it says that Unknown Member: A.
Can someone please help? Thanks.
Edit:I also have added the Microsoft.Csharp dll in the solution reference as per the screenshot:
I'm not sure which IDE did you use for developing your xamarin android app, by my side I used VS2015, and the error is
Missing compiler required member 'Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create'
This is because when use the dynamic keyword in your project. The assembly contains the C# runtime binder. To solve this issue, we need to add reference to Microsoft.CSharp library(Microsoft.CSharp.dll) in our project:
After adding this reference, your code runs well by my side.
But I'm not sure if the Xamarin.Android will limit the use of dynamic object, through I didn't find any limitation in xamarin's official document. For more information about dynamic object, you can refer to:
System.Dynamic.DynamicObject Class
System.Dynamic.ExpandoObject Class
Go to NuGet Package Manager > Manage NuGet packages menu. Then change Source to Microsoft Visual Studio Offline Packages. After adding, it should compile successfully.
Related
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.
I am trying to modify a class that I have obtained from an open source library. While I have referenced the library in my project, the original class contained references to parts of the library that are marked internal. This is causing build errors in my code.
I read this answer: How do you "override" an Internal Class in C#?. Note that it indicates that one is out of luck when wanting to access an internal class when one does not have access to the source code. This is, however, not the case for me.
I can fix this, but I want to know if there is a good way to do it. It appears to me that I have two options. I could modify the source code for the internal class so that it is not internal (bad), or I could import the class itself directly into my project (worse). If I import the class, there are further reference issues, and I will probably have to import the entire library to fix the dependencies.
Note that I am aware that I could add my change to the open source library and then build it as new library to reference in my code. I do not want to do this at this time because I would like to be able to step into my method in the debugger. Once I have debugged my method and assured its utility, I will make it available as part of my version of the open source project.
I am new to c# and this is my first time working on a large open source project so I would like some advise on how to deal with this issue.
Update:
Here is the link to the source code of the library that I would like to modify: https://github.com/accord-net/framework/blob/development/Sources/Accord.Math/Optimization/Unconstrained/Least%20Squares/LevenbergMarquardt.cs
To modify this code, I opened the source in visual studio and attempted to build the version as it currently exists. On line 304 the following code appears:
int[] block = Vector.Range(s * blockSize, s * blockSize + B);.
When I have this file in visual studio, it gives me an error saying that Vector is inaccessible due to its protection level. When I look at the definition of that code in the intellisense window, it shows that Vector is marked as internal.
If I attempt to bring the source code of Vector into my project, there are even more issued of the same type because Vector uses other internal classes.
Library version:
Are you sure that you're using the latest version of the library ?
By looking at the source of Vector.Range I can see that it is a public member.
Nuget packages of Accord.NET seems up to date, so check out your current version.
Additionally, I would recommend you to depend on the NuGet package instead of a manually installed assembly (you would see update notifications in Nuget Package Manager as a bonus).
Extending the library:
You have a couple of options here,
discuss with the authors about the possibility of opening some internal types, explain your needs and they might even suggest an alternative path without making such changes on their side
fork the library and make your changes, eventually PR
(personally I'd try step 1 first)
Debugging:
When referencing the Nuget package, you can create a PDB out assembly references using Telerik JustDecompile (freeware). Doing so relieves you to drag your own build and so on.
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.
I'm trying to build on my machine the System.Runtime.Caching project in Visual Studio 2015. The build fails because there is no definition for the classes:
private CacheExpires _expires;
private CacheUsage _usage;
CacheExpires and CacheUsage are not defined in this project and also not in any other project from the .net framework, and even in the Reference Source website there is no definition for those classes.
Those classes are used in MemoryCacheEntry.cs and MemoryCacheStore.cs
Has anyone succeed to build it? Am I doing something wrong or is this project not completely open source?
thanks!
You cannot build from Reference Source; it doesn't include build steps, generated code, localisation, and other things.
That's why it's called Reference Source.
I'm currently developing a Xamarin.Android application in VS2013.
I want to implement Azure Active Directory in my app following this guide. Then I add Microsoft.IdentityModel.Clients.ActiveDirectory nuget package as described.
Then I want to add an AuthenticationResult attribute, but then I got a missing assemble reference error. Even when I explicitely write (or drag and drop) Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationResult.
I looked everywhere for solutions, but none fits my problem. (Upgrade/Downgrade target framework didn't solve the issue)
If anyone has an idea about this, I would be really thankful because I'm getting out of ideas.
Thank you.
Few things to check or try:
Does the Reference to Microsoft.IdentityModel.Clients.ActiveDirectory appear in the References list from your solution?
If it appears, what are the properties, like copy to output, absolute/relative path?
Is it added for all Project configurations (Debug/Release)?
So I found out what was the problem. I had to use a specific version of the package since it's a pre-released version. I had to install 3.0.110281957 exactly then it recognize the reference as I needed.