我正在尝试dynamic在C#.net核心应用程序中使用一个变量,该应用程序的目标是.net标准1.6.(平台?库?框架?元框架?)我第一次在实际应用程序中遇到这个问题,但我把它减少到最小的再现.
{
"version": "1.0.0-*",
"buildOptions": { "emitEntryPoint": true },
"dependencies": { "NETStandard.Library": "1.6.0" },
"frameworks": {
"netstandard1.6": { "imports": "dnxcore50" }
},
"runtimes": { "win10-x64": {} }
}
Run Code Online (Sandbox Code Playgroud)
using System;
public class Program {
public static void Main(string[] args) {
dynamic hello = "hello world";
Console.WriteLine(hello);
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试构建它时,我在Console.WriteLine(hello);说这个时遇到了构建错误.
CS0656缺少编译器所需的成员'Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create'
是否可以dynamic在面向netstandard 1.6的应用程序中使用变量?怎么样?
我在我的<returns>一些方法中添加了xml标记,但是我无法在IntelliSense中看到它的内容.
这是我的代码:
/// <summary>
/// we all live in a yellow summary
/// </summary>
/// <returns>what it returns</returns>
public int MyMethod()
{ .... }
Run Code Online (Sandbox Code Playgroud)
有没有办法显示这些内容?
documentation intellisense xml-documentation visual-studio roslyn
看到我们可能会在下一个版本中获得这个功能,您认为自己能做什么或者想要使用此功能做些什么?
就个人而言,在代码生成期间(例如在MSBuild任务中),我有时希望能够在项目中查看代码,从而可以选择生成更好的代码(更多代码).
例如,如果我有一个具有部分方法的部分类,其中某个类型的参数被发送,那么在代码生成时我可以使用该信息生成更好的代码.
换句话说,我在编写代码时发布了我的"意图",我可以将代码生成器用于繁重的工作.
partial void InitCommandForStoredProc(ref DbCommand command, string storedProcName);
Run Code Online (Sandbox Code Playgroud)
在部分方法中给出此信息,我可以生成正确的代码.
那么你们都希望能够将Compiler作为一项服务做什么呢?
我有以下编译:
Solution solutionToAnalyze = workspace.OpenSolutionAsync(pathToSolution).Result;
var projects = solutionToAnalyze.Projects;
Compilation compilation = projects.First().GetCompilationAsync().Result;
var syntaxTrees = compilation.SyntaxTrees.First();
var semanticModel = compilation.GetSemanticModel(syntaxTree, true);
SyntaxNode newSource = semanticModel.SyntaxTree.GetRoot();
var methodRefactoringVisitor = new MethodRefactoringVisitor();
Run Code Online (Sandbox Code Playgroud)
我修改了一个方法的主体
public override SyntaxNode VisitMethodDeclaration(MethodDeclarationSyntax method)
{
var newBody = method.Body;
//modify newBody
var updatedMethod = method.ReplaceNode(method.Body, newBody);
return updatedMethod;
}
newSource = methodRefactoringVisitor.Visit(newSource);
Run Code Online (Sandbox Code Playgroud)
在对方法进行更改后,我想更新编译,以便例如我可以查询节点的类型:
var typeInfo = semanticModel.GetTypeInfo(node).Type;
Run Code Online (Sandbox Code Playgroud)
目前我正在做的事情:
var oldSyntaxTree = semanticModel.SyntaxTree;
var newSyntaxTree = newSource.SyntaxTree;
var newCompilation = compilation.ReplaceSyntaxTree(oldSyntaxTree, newSyntaxTree);
var newSemanticModel = newCompilation.GetSemanticModel(newSyntaxTree);
Run Code Online (Sandbox Code Playgroud)
我想在修改主体后立即更新编译,这样如果我从修改后的方法的父类调用访问者,我就可以看到更改.
是否可以在不编译整个项目/类的情况下部分更新编译? …
我正在尝试替换Roslyn语法树中的节点,它只是工作,但有一种烦恼,觉得它应该不是问题.
语法树是由脚本生成的,我要的结果是基于脚本的语法树太-但由于某些原因,在树替换节点创建与更改后的选项一种新的语法树:在Kind成为Regular代替Script.这是可以解决的,SyntaxTree.WithRootAndOptions但如果我需要调用它,感觉就像我做错了.
示例程序:
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Scripting;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Scripting;
using System;
using System.Linq;
class Program
{
static void Main(string[] args)
{
Script script = CSharpScript.Create("Console.WriteLine(\"Before\")",
ScriptOptions.Default.AddImports("System"));
var compilation = script.GetCompilation();
var tree = compilation.SyntaxTrees.Single();
var after = SyntaxFactory.LiteralExpression(
SyntaxKind.StringLiteralExpression,
SyntaxFactory.Literal("After"));
var root = tree.GetRoot();
var before = root.DescendantNodes().OfType<LiteralExpressionSyntax>().Single();
var newRoot = root.ReplaceNode(before, after);
var fixedTree = newRoot.SyntaxTree.WithRootAndOptions(newRoot, tree.Options);
Console.WriteLine(newRoot); // Console.WriteLine("After")
Console.WriteLine(tree.Options.Kind); // Script
Console.WriteLine(newRoot.SyntaxTree.Options.Kind); // …Run Code Online (Sandbox Code Playgroud) 回到2008年的PDC,在Anders Hejlsberg的C#期货谈话中,他谈到了重写C#编译器并提供"编译器即服务",当然我在他们针对C#4.0时间框架时给人的印象. ...
那么,有谁知道这是什么状态?它似乎没有在CTP中存在,除了链接到2008 PDC会话视频(大约一个小时)之外,几乎没有关于WEB的信息.
这个倡议变暗了吗?
我一直在阅读.NET编译器平台的贡献代码部分("Roslyn"),我遇到了编码约定的指南.我理解大多数编码约定以及为什么要求它.但我不明白他们的意思是:
避免在编译器热路径中进行分配:
避免使用LINQ.
避免在没有结构枚举器的集合上使用foreach.
什么是"编译器热门路径"?为什么我应该避免使用LINQ并避免对没有结构枚举器的集合进行预测?
我昨天在Visual Studio 2015中打开了我们的解决方案,我们的一些单元测试(在Visual Studio 2013中运行正常)开始失败.Digger更深入我发现这是因为召集GetTypes()一个程序集返回了不同的结果.我已经能够创建一个非常简单的测试用例来说明它.
在Visual Studio 2013和2015中,我使用.NET Framework 4.5.2创建了一个新的控制台应用程序.我在以下两个项目中都添加了以下代码.
class Program
{
static void Main(string[] args)
{
var types = typeof(Program).Assembly.GetTypes()
.Where(t => !t.IsAbstract && t.IsClass);
foreach (var type in types)
{
Console.WriteLine(type.FullName);
}
Console.ReadKey();
}
}
Run Code Online (Sandbox Code Playgroud)
当我在Visual Studio 2013中运行时,我得到以下输出(如预期的那样).
VS2013Example.Program
当我在Visual Studio 2015中运行时,我得到以下输出(不是预期的).
VS2015Example.Program
VS2015Example.Program + <>Ç
那是什么VS2015Example.Program+<>c类型的?原来它是.Where()方法中的lambda .是的,这是正确的,不知何故,本地lambda作为一种类型暴露.如果我.Where()在VS2015中注释掉,那么我就不再获得第二行了.
我已经使用Beyond Compare来比较两个.csproj文件,但唯一的区别是VS版本号,项目GUID,默认命名空间和程序集的名称,VS2015有一个对System.Net.Http的引用, VS2013一个没有.
有没有人见过这个?
有没有人解释为什么局部变量将在程序集级别作为类型公开?
c# compiler-construction roslyn visual-studio-2013 visual-studio-2015
最近我开始阅读有关.NET重组细节的信息(主要是通过.NET Core github页面).它接缝他们创建了兄弟项目以支持更多平台.在阅读时我的印象是CoreCLR和CoreRT是一个新的OpenSource版本的专有Roslyn编译器.CoreRT提供本机(AOT)编译.而LLILC是一种备选实现指挥LLVM框架.
任何人都可以从用户角度确认和描述这些项目的差异和目标吗?为什么有人会在未来使用Roslyn而不是CoreCLR?
我正在尝试使用一个拥有少量Roslyn Code Analyzers的大型开源项目.当我打开解决方案时,Visual Studio使用~35%的CPU大约15分钟.使用PerfView我发现在解决方案上运行的代码分析器正在使Visual Studio陷入困境.
我知道可以在每个项目的基础上禁用分析器,但是这个解决方案包含100多个项目,所以我不想一个接一个地执行此操作.
我的问题: