Roslyn - namespaces - c#

Can anyone tell what namespace the Syntax class is in?
I have installed all the roslyn packages through nuget but I don't know what namespace the static factory methods for creating expression syntax objects is in.
Also a lot of the examples of Roslyn on the web are using these namespaces
using Roslyn.Compilers;
using Roslyn.Compilers.CSharp;
Are these obsolete now? I am using the following
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
Can somebody explain the difference?

From what I can gather the namespaces are obsolete now.
SyntaxFactory replaces Syntax if anyone is interested.

Related

Why grammar does not highlight keyword (Visual Studio Language Server)

ITNOA
I want to write Kamailio Language Server for Visual Studio 2022, I Create a project in GitHub with below structure
And I use kamailio.tmLanguage.json from https://github.com/miconda/vscode-kamailio-syntax/blob/master/syntaxes/kamailio.tmLanguage.json that I sure works for Visual Studio Code, So this grammar is correct
But I do not know why my code is not working correctly and does not highlight keyword?
My test with .kcfg file extension like below
using Microsoft.VisualStudio.LanguageServer.Client;
using Microsoft.VisualStudio.Utilities;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Kamailio.VisualStudio
{
#pragma warning disable 649
public class KamailioContentDefinition
{
[Export]
[Name("kamailio")]
[BaseDefinition(CodeRemoteContentDefinition.CodeRemoteContentTypeName)]
internal static ContentTypeDefinition KamailioContentTypeDefinition;
[Export]
[FileExtension(".kcfg")]
[ContentType("kamailio")]
internal static FileExtensionToContentTypeDefinition KamailioFileExtensionDefinition;
}
#pragma warning restore 649
}
Any body can find my mistake?
All code to reproduce my problem is in GitHub
The good news:
It seems that all you need to do is add fileTypes to your kamailio.tmLanguage.json file, e.g.
"fileTypes": [
".cfg"
],
VS matches TextMate grammars to files based on these properties.
The bad news:
VS is apparently not smart enough to support other ways to filter when you are or aren't applicable. Specifically, it does not seem to support the firstLineMatch property, so it will apply your grammar to all *.cfg files. If there is another way to filter which files are or are not applicable, I wasn't able to find it.

Why is the reference for ConfigurationErrorsException missing from my .Net 3.0 application?

I have a .NET 3.0 Winform Application with a corrupt user.config file causing system config errors.
I wanted to improve handle these errors, similar to how it's done in this example on CodeProject.
To use '
However, when I try and use the 'ConfigurationErrorsException' to get the name of corrupted file I receive the following error:
The type or namespace name 'ConfigurationErrorsException' could not be found (are you missing a using directive or an assembly reference?)
I have only seen this error previously where there were .NET framework conflicts.
The project has the target framework .NET 3.0. The ConfigurationErrorsException was added in 2.0, so I didn't think there would be a framework conflict there.
The version 3.0 documentation is lacking compared to other versions and does say:
The current value is not one of the EnableSessionState values.
Which I'm not entirely sure what that implies (the documentation for EnableSessionState lead to more confusion rather than less).
Why does the program fail to build, with a type or namespace missing error?
Is this related to targeting .NET 3.0?
If so, Is there an alternative for .NET 3.0?
EDIT:
I have made sure to include 'using System.Configuration' at the top of my page. The total list of usings is:
using System;
using System.Collections.Generic;
//using System.Linq;
//using System.Threading.Tasks;
using System.Configuration;
using System.Windows.Forms;
using System.Threading;
using ProjectName.Properties;
using System.Security.Permissions;
using Microsoft.Win32;

Why can't I reference System.Runtime.Serialization.Json in C#

I want to use an API to get info from the interwebz. The API returns data in Json format.
I'm running Microsoft Visual Studio C# 2010 Express addition.
It appears that I have the .NET Framework 4 Client Profile set as my
"Target framework" but I'm honestly not sure exactly what this
means.
This is a Windows Forms Application...
Not much code to show because I can't really get started without the appropriate using statement...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Net;
using System.Runtime.Serialization.Json;
I get this error:
The type or namespace name 'Json' does not exist in the namespace
'System.Runtime.Serialization' (are you missing an assembly
reference?)
Am I missing a DLL file or something? Based on my hours of fruitlessly searching for solutions, I understand that the .NET 4.xx should already have the tools needed to parse up a Json formatted string?
The System.Runtime.Serialization.Json Namespace is in two different DLL's depending on your .net framework.
In .NET 3.5 It is in System.ServiceModel.Web.dll
In .NET 4.0 and above It is in System.Runtime.Serialization.dll.
Make sure you have added the correct DLL as a reference in your project and add using System.Runtime.Serialization.Json; to the top of your code file.
EDIT - Consider using JSON.NET
Even though the .NET Framework supplies its own JSON Serialization and Deserialization namespaces (DataContractJsonSerializer and JavaScriptSerializer) you should investigate whether you would be better off using JSON.NET.
JSON.NET is easier to use, better performance and has far more features.
http://www.newtonsoft.com/json/help/html/JsonNetVsDotNetSerializers.htm
you need to import System.Runtime.Serialization dll from reference
You need to add a reference to your project.
In the Solution Explorer right click references then add reference. You'll see a list of DLL's and you have to check the box next to the one you need for it to be added to the project. After you've done this you can successfully add the using statement.
Hope that helps!
The general process for serializing and deserializing JSON from C# is:
Add a reference to the System.Runtime.Serialization library.
Add using directives for System.Runtime.Serialization and System.Runtime.Serialization.Json.
Please change your Target framework from .NET Framework 4 Client Profile to .NET Framework 4
I know this is an old question, but I came across this in .NET 5.0 and the solution is to add using System.Text.Json to the top of your code.

type or namespace InterpolationMode not found

I have a .net application and now I am trying to build it to mobile application but I am getting an error as:
"type or namespace InterpolationMode not found"
But I have included all references,
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
Please let me know what I have to include?
InterpolationMode is not supported in the Compact Framework (nor are any method overloads that accept it, so adding it would be of no use). Without seeing how you're using it, it's very difficult to recommend a workaround.
Include namespace - using System.Drawing.Drawing2D;
Also see - MSDN
It's System.Drawing.dll according to MSDN. Maybe mobile SDK doesn't include it? I'm not sure

Referencing namespaces globally?

Is there a way to reference a namespace globally across the whole solution?
So instead of having these lines in every code file:
using System;
using MyNamespace;
having to declare them only once, and every code file would use them.
Btw I am using Visual Studio.
No, C# doesn't have this concept. Each source file is independent in this respect. (And if the using directives are in a namespace declaration, those are independent from other using directives in peer namespace declarations, too. That's a pretty rare case though in my experience.)
You don't need ReSharper to change what gets included in a new class though. You can use the Visual Studio templates.
EDIT: Just to clarify the point about using directives within namespaces, suppose we had (all in one file):
using Foo;
namespace X
{
using Bar;
// Foo and Bar are searched for code in here, but not Baz
}
namespace Y
{
using Baz;
// Foo and Baz are searched for code in here, but not Bar
}
Usually I only have one namespace declaration in a file, and put all the using directives before it.
No, this is not possible.
If you're using ReSharper, you can set an option to include specific using directives in every new file you create though.
From this SO question and follow-up blog post. You can edit the Visual Studio default templates.
To do this, look at the file in this zip : [Program Files][Visual Studio]\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip
and modify the Class.cs file as needed. Additionally, Visual Studio may have cached this file here :
[Program Files][Visual Studio]\Common7\IDE\ItemTemplatesCache\CSharp\Code\1033\Class.zip
In C# 10.0 you can use Global Usings.
global using System;
global using MyNamespace;
No, you can not reference a namespace globally across the whole solution in .NET or .NET CORE.
But you can use project wise namespace globally in solution. this feature will be available from c#10/.NET 6. currently it's in preview but it will be released in NOV 2021
=========Project level .NET 6 global using namespace=========
Create a class file at root of the project e.g GlobalNamespace.cs
global using System;
global using System.Linq;
global using System.Text.RegularExpressions;
global using System.Threading.Tasks;
Then you don't need to declare using namespace in other .cs files of the project which are already declared globally.
As others have mentioned Visual Studio Templates are the way to go.
Note that simply adding a using statement to your template will not ensure that the compiler can resolve your types. So, if you are adding a using statement for MyNamespace in every class you may need to add an assembly reference to your project as well. See the C# FAQ for more information.
One trick I miss as a newb to CSharp is to look at the "refences" (in VS), to right click and "Add New Reference". This is especially handy when combining mulitple projects where I have made some generic class for reuse elsewhere.

Categories

Resources