roslyn

VS2015 crashing with “A new guard page…” when just editing source code

瘦欲@ 提交于 2021-02-10 05:33:50
问题 The Problem: Visual Studio 2015 keeps crashing with this error: A new guard page for the stack cannot be created Background: There are already a few questions out there regarding this error in other contexts, but they all appear to be related to interoperating with legacy COM code or designer tools in Visual Studio. I am just trying to edit a C# source file. Windows service / A new guard page for the stack cannot be created A new guard page for the stack cannot be created Previously, I've

How can I find previous usages of a variable using Roslyn?

本小妞迷上赌 提交于 2021-02-08 02:57:32
问题 I'm writing a Rosyln analyser/analyzer. It checks to ensure that a method is called before accessing another (potentially dangerous) method on a type. To show what I mean, here's some bad code that I want to analyse and fail on: private void myMethod() { var myThing = new MyThing(); myThing.Value = null; string value = myThing.GetValue(); // code blows up here as the internal value is null } Here's code that's OK because it calls a method that says whether it's null: private void myMethod() {

How to create a Roslyn ITypeSymbol for an arbitrary type?

我与影子孤独终老i 提交于 2021-02-07 08:27:56
问题 I can use a SyntaxGenerator to generate a parameter of type Int32 like so... var generator = SyntaxGenerator.GetGenerator(document); var paramType = generator.TypeExpression(SpecialType.System_Int32); var param = generator.ParameterDeclaration("MyParam", paramType); What equivalent code should I use to create a parameter of type Dataset ? I presume I need to create an ITypeSymbol to pass to the generator.TypeExpression , but how to do this? 回答1: If you have access to a Compilation , you can

How to create a Roslyn ITypeSymbol for an arbitrary type?

隐身守侯 提交于 2021-02-07 08:27:50
问题 I can use a SyntaxGenerator to generate a parameter of type Int32 like so... var generator = SyntaxGenerator.GetGenerator(document); var paramType = generator.TypeExpression(SpecialType.System_Int32); var param = generator.ParameterDeclaration("MyParam", paramType); What equivalent code should I use to create a parameter of type Dataset ? I presume I need to create an ITypeSymbol to pass to the generator.TypeExpression , but how to do this? 回答1: If you have access to a Compilation , you can

[译]C# 7系列,Part 2: Async Main 异步Main方法

对着背影说爱祢 提交于 2021-02-05 02:47:07
原文: https://blogs.msdn.microsoft.com/mazhou/2017/05/30/c-7-series-part-2-async-main/ 你大概知道,C#语言可以构建两种程序。 一种是带有入口点(entrypoint)的程序,这样操作系统就可以加载程序并从入口点执行; 另一个是没有入口点的程序。 操作系统不能直接执行程序,程序可以被其他有入口点的程序引用,这样就可以执行其中的代码。 必须有入口点的应用程序类型有:Windows Forms应用程序、UWP应用程序、控制台应用程序、WPF应用程序、ASP.NET 和ASP. NET Core应用程序和Xamarian App。 不需要入口点的应用程序类型有:类库(/t:lib)、模块(/t:module)。 Main方法 像其他语言一样,C#程序从Main方法开始。 有四个重载被认为是Main方法的有效签名。 public static void Main(); public static int Main(); public static void Main( string [] args); public static int Main( string [] args); C#的入口点方法必须是静态的,方法名必须是Main,这个方法的返回类型可以是void或者int,它可以有一个字符串数组的参数

Visual Studio 2019 (16.4.2) crashed without being able to open solutions

心不动则不痛 提交于 2021-01-29 19:05:55
问题 I just see 16.4.2 released, but after I upgraded my VS 2019, as soon as I try to open a solution I got error The 'RoslynPackage' package did not load correctly. The problem may have been caused by a configuration change or by the installation of another extension. You can get more information by examining the file 'C:\Users\my-user\AppData\Roaming\Microsoft\VisualStudio\16.0_db5aa3ff\ActivityLog.xml'. Restarting Visual Studio could help resolve this issue. When I open ActivityLog.xml and

Convert a C# string value to an escaped string literal with Roslyn

左心房为你撑大大i 提交于 2021-01-28 06:11:33
问题 There is such method for CodeDom by @Hallgrim found here: private static string ToLiteral(string input) { using (var writer = new StringWriter()) { using (var provider = CodeDomProvider.CreateProvider("CSharp")) { provider.GenerateCodeFromExpression(new CodePrimitiveExpression(input), writer, null); return writer.ToString(); } } } Nowadays we need a Roslyn remake for .NET Core. Or should we manually replace symbols? 回答1: You can create a SyntaxNode ( LiteralExpressionSyntax ) from the input

Visual Studio IDE0059 C# Unnecessary assignment of a value bug?

烂漫一生 提交于 2021-01-27 06:10:39
问题 I have the following C# Code (I reduced it to the bare minimum to simplify it). Visual Studio 2019, .NET Framework 4.7.2. public void Demo() { ReportStart(); var success = false; try { int no = 1; switch (no) { case 1: default: break; } DoSomething(); success = true; } finally { ReportEnd(success); } } From my understanding, there is nothing wrong about it. The function may fail (I don't want to catch it) but before leaving, it will report successful execution to another method. When

欢迎来到 C# 9.0(Welcome to C# 9.0)

故事扮演 提交于 2021-01-05 23:49:39
翻译自 Mads Torgersen 2020年5月20日的博文《Welcome to C# 9.0》,Mads Torgersen 是微软 C# 语言的首席设计师,也是微软 .NET 团队的项目群经理。 C# 9.0 正在成形,我想和大家分享一下我们对下一版本语言中添加的一些主要特性的想法。 对于 C# 的每一个新版本,我们都在努力让常见的编码场景的实现变得更加清晰和简单,C# 9.0 也不例外。这次特别关注的是支持数据模型的简洁和不可变表示。 就让我们一探究竟吧! 一、仅初始化(init-only)属性 对象初始化器非常棒。它们为类型的客户端提供了一种非常灵活和可读的格式来创建对象,并且特别适合于嵌套对象的创建,让你可以一次性创建整个对象树。这里有一个简单的例子: new Person { FirstName = "Scott" , LastName = "Hunter" } 对象初始化器还使类型作者不必编写大量的构造函数——他们所要做的就是编写一些属性! public class Person { public string FirstName { get ; set ; } public string LastName { get ; set ; } } 目前最大的限制是属性必须是 可变的(即可写的) ,对象初始化器才能工作:它们首先调用对象的构造函数

ASP.NET: Roslyn slow on page changes

半城伤御伤魂 提交于 2020-12-29 09:48:00
问题 i have an VB.NET ASP.NET (4.6.1) MVC Application which is build with the new Roslyn Compilers. Everytime i change a (vbhtml) File and reload the page it takes approx 9 seconds to reload the page. In this time a vbc.exe and a VBCSCompiler.exe are running: I couldn't find any hints for this slow performance (using ANTS Profile) and my System should be fast enough to handle the Requests much quicker (Windows 10 x64, i7-2600, 16GB RAM, Samsung SSD for all files). i also tried the new Roslyn