Syntax Analysis with MS Roslyn - c#

I have a litle but stupid problem. I started working with MS Roslyn, and I am trying to do the Walkthrough, but directly at the beginning an error occurred...
error CS0117: 'Roslyn.Compilers.CSharp.SyntaxTree' does not contain a
definition for 'ParseCompilationUnit'
I do not understand why it occurred... maybe one of you had the same problem.
My Sourceode:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Roslyn.Compilers;
using Roslyn.Compilers.CSharp;
using Roslyn.Services;
using Roslyn.Services.CSharp;
namespace gettingstarted2
{
class Program
{
static void Main(string[] args)
{
SyntaxTree tree = SyntaxTree.ParseCompilationUnit(
#"using System;
using System.Collections;
using System.Linq;
using System.Text;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(""Hello, World!"");
}
}
}");
var root = (CompilationUnitSyntax)tree.GetRoot();
}
}
}

What is the date mentioned in the walkthrough you were following? The method was renamed from ParseCompilationUnit to ParseText for the September CTP. If you a previous CTP installed, it's possible that the walkthrough wasn't updated properly when you installed the latest CTP.
I would recommend uninstalling and reinstalling the CTP or using repair.

#Anton to be shure, that is the newest one... You can use Nuget to install Roslyn.
Run the following command in the Package Manager Console : PM> Install-Package Roslyn

Related

what reference I need to add in C# to get LaunchPadApi?

I try to connect to the API (Using C#) that client provided, but it is using LaunchPadApi() I am not sure what assembly I need to add to get this, these are 2 references that were added:
using IO.Swagger.Api;
using IO.Swagger.Client;
I was able to find using IO.Swagger.Client; but I am not able to find using IO.Swagger.Api;
This is a sample code I am not sure how I can get LaunchPadApi defined so "responsePost" method is not recognized as well,
using System;
using System.Diagnostics;
using IO.Swagger.Api;
using IO.Swagger.Client;
using IO.Swagger.Model;
public class responsePostExample
{
public void main()
{
var apiInstance = new LaunchPadApi();
....
try
{
apiInstance.responsePost(body, xAPIKey);
}
....
Install Launchpad in your nuget package manager for the project
Add using Launchpad; to the top of your file

setup ironpython: Module not found

I'm using Ironpython for the first time to Import a Python Script to C#. I get the error "No module named numpy", but I don't know why. I read that I have to add my path of my modules to my python script. This is my python script:
import numpy as np
import sys
sys.path.append(r"C:\Users\abc\CSharp\PythonScriptExecution1\packages\IronPython.2.7.9\lib")
sys.path.append(r"C:\Users\abc\PycharmProjects\untitled3\venv\Lib")
sum = np.sum([0.5, 1.5])
print(sum)
The second path is the path which is also used as project interpreter in Pycharm for the python.exe.
My C# code is this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PythonScriptExecution2
{
class Program
{
static void Main(string[] args)
{
Microsoft.Scripting.Hosting.ScriptEngine pythonEngine =
IronPython.Hosting.Python.CreateEngine();
// We execute this script from Visual Studio
// so the program will be executed from bin\Debug or bin\Release
Microsoft.Scripting.Hosting.ScriptSource pythonScript =
pythonEngine.CreateScriptSourceFromFile("C:/Users/abc/PycharmProjects/untitled3/test.py");
pythonScript.Execute();
}
}
}
Running the Python script in Pycharm works fine, but importing it to C# results in the error mentioned above. Can someone help me how to set the right paths?
edit: If it doesn't work, does anyone know any other way to run a python script with C#?
Related to How to add modules to Iron Python?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PythonScriptExecution2
{
class Program
{
static void Main(string[] args)
{
Microsoft.Scripting.Hosting.ScriptEngine eEngine =
IronPython.Hosting.Python.CreateEngine();
// We execute this script from Visual Studio
// so the program will be executed from bin\Debug or bin\Release
Microsoft.Scripting.Hosting.ScriptSource pythonScript =
ICollection<string> searchPaths = engine.GetSearchPaths();
searchPaths.Add(#"C:\Users\abc\CSharp\PythonScriptExecution1\packages\IronPython.2.7.9\lib");
searchPaths.Add(#"C:\Users\abc\PycharmProjects\untitled3\venv\Lib");
engine.SetSearchPaths(searchPaths);
engine.CreateScriptSourceFromFile("C:/Users/abc/PycharmProjects/untitled3/test.py");
pythonScript.Execute();
}
}
}

Error when getting a UWP package info from a Winform App?

Using a WinForm App, I'm trying to mimic this sample from Microsoft's Github site that shows how to get package info by using the Windows Runtime packaging API.
I'm getting following error at line: Package package = Package.Current;, of the code below, when trying to get a UWP package info from a WinForm app:
The type or namespace name 'Package' could not be found (are you missing a using directive or an assembly reference?)
Question: Although the error is a famous C# error that has many online posts/solutions, but here the context is different. Compiler seems to be complaining that I'm missing required assembly for Package class. But I do have using Windows.ApplicationModel; using statement in my code below. So what may be a possible cause of the error; i.e. what I may be missing here?
NOTE: To ensure the inclusion of the required assemblies, I did install this UWPDesktop NuGet package in WinForm Project on VS2017-ver 15.9.5 on Windows 10 Pro - Ver 8109:
WinForm App: Relevant code that throws error at line: Package package = Package.Current;
using System;
using System.Windows.Forms;
using Windows.ApplicationModel; //I added from here
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
using Windows.ApplicationModel.Background;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.Storage.Search;
using Windows.UI.Xaml;
using Windows.Management.Deployment;
namespace WinForms_to_UWP
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Package package = Package.Current;
PackageId packageId = package.Id;
Console.WriteLine(packageId.FullName);
}
}
}
From the GitHub's UWP Sample project: The relevant Code from scenario1_identity.xaml.cs that WORKS fine:
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
using SDKTemplate;
using System;
using Windows.ApplicationModel;
namespace SDKTemplate
{
void GetPackage_Click(Object sender, RoutedEventArgs e)
{
Package package = Package.Current;
PackageId packageId = package.Id;
String output = String.Format("Name: \"{0}\"\n" + packageId.FullName);
OutputTextBlock.Text = output;
}
UPDATE:
Also worth noticing that when adding Using Windows..... statements on the top, the VS intellisense recognized only Window.Foundation and Window.UI. statements. For other Using Windows..... statements I had to hardcode - for example, Windows.ApplicationModel;. However VS2017 did not complain when I hard coded them. Moreover, all Using statements starting with Windows. are grayed out as shown in image below. Not sure if it has anything to do with the error:
The Nuget package you are referencing is outdated I am afraid and may not be maintained anymore.
But the problem is easy to fix. Just add a reference to the windows.winmd file of the SDK version you are targeting. See this screenshot:

Issues with Intellisense and class library

I am using Visual Studio 2013. I am trying to start unit testing by using this tutorial.
I have added a class library and a reference to MVC. However, the Intellisense/Autocompletion is not working properly within my class library. Currently, this is all the code I have in my test class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using System.Web.Mvc;
using System.Web;
using Application_Portal;
using Application_Portal.Controllers;
namespace ApplicationPortalTests
{
[TestFixture]
public class HomeControllerTest
{
HomeController home = new HomeController();
}
}
The Intellisense does not seem to recognize the home variable (no suggested properties, etc.) Typing home.Index() gives the following error:
ApplicationPortalTests.HomeControllerTest.home' is a 'field' but is used like a 'type'
Furthermore, it does not even seem to recognize "var" (as in var result = ...). Instead, when I type var and hit space, it autocompletes it as EnvironmentVariableTarget.
I have tried cleaning and rebuilding the project, and closing and reopening Visual Studio, but with no success.
What might the issue be? I appreciate any advice.
You have declared your variable inside the class. If you want to use this variable, it must be within the context of a member. For instance:
[Test]
public void test_my_index_page()
{
var result = home.index();
}

TcpChannel could not be found

I'm following a lab tutorial on .NET remoting and for that I need to create TcpChannel object. For that I have added using System.Runtime.Remoting namespace but it still gives me an error The type or namespace name 'TcpChannel' could not be found (are you missing a using directive or an assembly reference?)
I'm using Visual Studio 2015 Community Edition. I can't figure out what is causing this problem. I even googled it. Here's my code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Remoting;
namespace RemotingServer
{
class Program
{
static void Main(string[] args)
{
TcpChannel channel = new TcpChannel(8001);
}
}
}
The TcpChannel class is located in System.Runtime.Remoting.Channels.Tcp, so you have to get using that. Also check if you reference System.Runtime.Remoting.dll in your project.

Categories

Resources