Roslyn - Can't find SyntaxNode.ReplaceNode() - c#

Following this tutorial http://msdn.microsoft.com/en-us/vstudio/hh543922.aspx , I'm trying to use the ReplaceNode method that should be in the SyntaxNode class.
The thing is, I have this error: "Roslyn.Compiler.CSharp.SyntaxNode does not contain a definition for 'ReplaceNode'
Any ideas?

ReplaceNode is actually an extension method (so that it can return the type passed in). Make sure you have using Roslyn.Compilers; and using Roslyn.Compilers.CSharp;

Looks like an issue with that version of the CTP. Others are having the same issue, such as http://youtrack.jetbrains.com/issue/DOTP-4774 or http://youtrack.jetbrains.com/issue/DOTP-4836 .

Related

System.CommandLine Command custom validator: how to search

I am writing a C# console application project (.NET 7.0) using Visual Studio for macOS 2022 v17.4 and System.CommandLine (2.0.0-beta4.22272.1).
I am trying to add a custom validator to one of my Commands, and found the following code snippet on the project's GitHub site:
this.AddValidator(commandResult =>
{
if (commandResult.Children.Contains("one") &&
commandResult.Children.Contains("two"))
{
commandResult.ErrorMessage = "Options '--one' and '--two' cannot be used together.";
}
});
However, it will not compile in my command, due to Error CS1929: 'IReadOnlyList' does not contain a definition for 'Contains' and the best extension method overload 'MemoryExtensions.Contains(ReadOnlySpan, string)' requires a receiver of type 'ReadOnlySpan' (CS1929).
I have looked for the appropriate extension method in the various System.CommandLine namespaces, but I don't see anything. (Yes, I included the System.Linq namespace.)
Can anyone suggest what is amiss?
Edit: The original code which I "borrowed" is this:
command.AddValidator(commandResult =>
{
if (commandResult.Children.Contains("one") &&
commandResult.Children.Contains("two"))
{
return "Options '--one' and '--two' cannot be used together.";
}
return null;
});
Note that this delegate returns a value, while the updated one does not, but instead sets ErrorMessage. That's one breaking change in System.CommandLine, and it's plausible that the issue here is due to another breaking change. The System.CommandLine project does note that the project is in a state of flux and subject to changes.
I have no experience with this specific library, but peeking its source hints that the collection you are calling .Contains() on doesn't contain Strings, but rather instances of SymbolResult, hence it cannot find suitable overload for the method you are trying to call.
I couldn't find the code you provided anywhere from mentioned GitHub repo, but instead found this piece of code from this test file of library:
command.Validators.Add(commandResult =>
{
if (commandResult.Children.Any(sr => sr.Symbol is IdentifierSymbol id && id.HasAlias("--one")) &&
commandResult.Children.Any(sr => sr.Symbol is IdentifierSymbol id && id.HasAlias("--two")))
{
commandResult.ErrorMessage = "Options '--one' and '--two' cannot be used together.";
}
});
The extension method you need is defined as part of the class System.Linq.Enumerable. So you need using System.Linq; at the top of your class file.
But that won't help if your project doesn't reference the assembly System.Linq.dll. (I don't know why it wouldn't, just out of the box, but perhaps it doesn't.) So you may need to add a reference for that.

Why use the following statement using aName = library in c#?

I was searching a ZigBee library for c# and I found GBee. I took a look to the code and I've found the following statement:
using AtCmd = NETMF.OpenSource.XBee.Api.Common.AtCmd;
Why is used this statement and what means?
Searching into the code I also found that AtCmd is used as a "class" to send standard command e.g.(AtCmd.RestoreDefaults,AtCmd.NetworkReset)
This meas, that all AtCmd statements in code will become from NETMF.OpenSource.XBee.Api.Common namespace. It simply means 'default namespace' for this type.
Probably you have in code two exact names, but deriving from different namespaces.
From MSDN:
This is called a using alias directive

FizzlerEx/HtmlAgilityPack QuerySelectorAll not working

I've been attempting to set up FizzlerEx, found at http://fizzlerex.codeplex.com/. After adding the references to my project, I've attempted to run the example code given on the website- the entirety of my code is listed below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HtmlAgilityPack;
using Fizzler.Systems.HtmlAgilityPack;
namespace Fizzler_Test
{
class Program
{
static void Main(string[] args)
{
var web = new HtmlWeb();
var document = web.Load("http://example.com/page.html");
var page = document.DocumentNode;
foreach (var item in page.QuerySelectorAll("div.item"))
{
var title = item.QuerySelector("h3:not(.share)").InnerText;
var date = DateTime.Parse(item.QuerySelector("span:eq(2)").InnerText);
var description = item.QuerySelector("span:has(b)").InnerHtml;
}
}
}
However, this yields build errors, claiming that:
Error 1 'HtmlAgilityPack.HtmlNode' does not contain a definition for 'QuerySelectorAll' and no extension method 'QuerySelectorAll' accepting a first argument of type 'HtmlAgilityPack.HtmlNode' could be found (are you missing a using directive or an assembly reference?)
It would seem that QuerySelectorAll is not actually a part of HtmlNode, but given that this is the official example code taken verbatim from the website, I'd expect the creators understand how their library works. I'm at a loss as to what the actual issue could be.
A related problem seems to have been found here, but no suitable answer was ever found: Fizzler and QuerySelectorAll
It would seem that QuerySelectorAll is not actually a part of HtmlNode, but given that this is the official example code taken
verbatim from the website, I'd expect the creators understand how
their library works. I'm at a loss as to what the actual issue could
be.
You are correct about this part. Though you are not correct about the second part, since the author of HAP isn't the author of FizzlerEx. The problem is elsewhere.
Simply by looking at the error, you get the only clue you need to go to solve this .
Error 1 'HtmlAgilityPack.HtmlNode' does not contain a definition for 'QuerySelectorAll' and no extension method 'QuerySelectorAll' accepting a first argument of type 'HtmlAgilityPack.HtmlNode' could be found (are you missing a using directive or an assembly reference?)
So, what do we get; it tells us that there is no method called QuerySelectorAll in the class HtmlNode in the namespace HtmlAgilityPack. If we take a look at the source code of HAP, you can easily determine that the error message is indeed correct, since there is no method by that name in the class we are looking.
Source code for HtmlAgilityPack.HtmlNode - class
Where is this method that we want to use, but cannot find?
It's here, In the Fizzler.Systems.HtmlAgilityPack.HtmlNodeSelection-class.
After trying a few things, I get the code to work perfectly just as it is. The problem was the extensions in the reference between Fizzler and HAP source code.
If you download Fizzler you get HtmlAgilityPack at the same time. When you add the references in Visual Studio (assuming you use that), only add
Fizzler.Systems.HtmlAgilityPack.dll
HtmlAgilityPack.dll
Clean your solution and rebuild it and it should work!
You should add Fizzler by right clicking on references -> Manage Nuget Package, and search online for it, you will find it as Fizzler for HtmlAgilityPack, and then can download it.

cant use linq "Last()" method

I dont know a whole lot about C# but I have this project I am working on where I would like to do something like this:
SortedDictionary<int, List<ChessMove>> possibleMovesByRank = new SortedDictionary<int, List<ChessMove>>();
...
var best = possibleMovesByRank.Keys.Last();
From what I have been able to find, this should use linq to return the key that has the highest value, however VS is giving me an error:
SortedDictionary.KeyCollection does not contain a definition for 'Last' and no extension method for 'Last'
Am I missing something or is my project not set up correctly or something?
Most likely, you are missing the System.Linq namespace in your C# file. This is an Extension Method and Enumerable.Last will not exist unless you include the relevant namespace.

Why can't I use the following IEnumerable<string>?

I'm getting the following error:
Error 25 The type or namespace name 'IEnumerable' could not be found (are you missing a using directive or an assembly reference?) C:\Development\Leverage\Leverage\Reports\SurveyLevel.aspx.cs 39 17 Leverage
because of this line:
private IEnumerable<string> GetDateParameters()
How do I deal with this? I tried to add in the line:
using System.IDisposable
at the top, but this doesn't fix it.
As others have said, you're missing using System.Collections.Generic;.
But that's giving you a fish; we should be teaching you to catch your own fish.
The way to solve this problem on your own is:
Enter the name of the type into your favourite search engine, and see what comes back:
IEnumerable(T) Interface (System.Collections.Generic)
http://msdn.microsoft.com/en-us/library/9eekhta0
Exposes the enumerator, which supports a simple iteration over a collection of a specified type.
See the bit that I highlighted in bold there? That's the namespace that you're missing.
If you still get the error then you are likely missing a reference; you can find out which DLL you have failed to reference by clicking on the link and reading the documentation page; it will tell you which DLL to reference.
You are missing a using System.Collections.Generic; statement at the top of the code file.
The generic IEnumerable<T> type cannot be found directly.
You could declare the full name instead:
private System.Collections.Generic.IEnumerable<string> GetDateParameters()
IEnumerable is in System.Collections
IEnumerable<T> is in System.Collections.Generic
You just need to add System.Collections.Generic namespace top of your code.
IEnumerable<T> belongs on this namespace in mscorlib.dll assembly.
You can use it like;
private System.Collections.Generic.IEnumerable<string> GetDateParameters()
Above answers are good. In my case, even after following the above answers it did not resolve the issue. Still the red squiggly constantly appeared.
Issue was the Framework of the project. It was by default set to .NET Framework 4.0.3 and changing to .NET Framework 4.0.0 will also help.
Save your Project properties after the change, build and it should all work.
Hope this helps.

Categories