Tag: roslyn

为什么我不能从表达式身体成员中抛出exception?

使用表达式成员允许您将方法或属性的主体定义为单个表达式而不使用return关键字(如果它返回了某些内容)。 例如,它变成了这些 int Method1() { return 5; } void Method2() { Console.WriteLine(); } 进入这些 int Method1() => 5; void Method2() => Console.WriteLine(); 当您从正文中抛出exception时,会产生差异: void Method3() { throw new Exception(); } 但是,以下内容将无法编译: void Method3() => throw new Exception(); 以下消息: Warning The member ‘Program.Exception()’ does not hide an inherited member. The new keyword is not required. Error […]

Roslyn可以从对象实例生成源代码吗?

在Visual Studio 2015中使用Roslyn API,我可以将对象实例转换为源代码吗? 我可以创建像“.ToSourceCode()”这样的扩展方法,如下所示吗? class Foo { } class Program { static string classSourceCode = “class Foo { }”; static void Main() { var instance = new Foo(); var instanceSourceCode = instance.GetType().ToSourceCode(); System.Diagnostics.Debug.Assert(instanceSourceCode == classSourceCode); } }

从罗斯林的符号中获取类型

对于不同类型的符号,从Microsoft.CodeAnalysis.ISymbol获取System.Type的最佳通用方法是什么? (例如类声明,变量,属性等) 我希望能够对类型进行各种检查,例如检查类型是否实现任何接口,或者是否可以转换为任何接口,就像可以检查System.Type一样。 我遇到的问题是,用于表示符号的大多数具体类都是内部的(请参阅http://source.roslyn.io/ ),我在ISymbol中找不到tye类型信息。 SourceNamedTypeSymbol LocalSymbol 我使用以下代码检索ISymbol var objectSymbol = (ISymbol)model.GetDeclaredSymbol(obj.Node);

加载由Roslyn编译器生成的程序集

我正在使用Roslyn编译器生成Greeter.dll。 尝试加载DLL文件时出现问题。 这是代码: using System; using Roslyn.Compilers; using Roslyn.Compilers.CSharp; using System.IO; using System.Reflection; using System.Linq; namespace LoadingAClass { class Program { static void Main(string[] args) { var syntaxTree = SyntaxTree.ParseCompilationUnit(@” class Greeter { static void Greet() { Console.WriteLine(“”Hello, World””); } }”); var compilation = Compilation.Create(“Greeter.dll”, syntaxTrees: new[] { syntaxTree }, references: new[] { new AssemblyFileReference(typeof(object).Assembly.Location), […]

使用带有ModuleBuilder而不是MemoryStream的Roslyn Emit方法

使用Roslyn编译为动态程序集时,我遇到了性能问题。 编译花了大约3秒,而使用CodeDom编译器编译相同的代码大约需要300毫秒。 这是我用于编译的代码的简化版本: var compilation = CSharpCompilation.Create( “UserPayRules.dll”, syntaxTrees, assembliesToAdd); using (var stream = new MemoryStream()) { stopWatch.Start(); var result = compilation.Emit(stream); stopWatch.Stop(); Debug.WriteLine(“Compilation: {0}”, stopWatch.ElapsedMilliseconds); if (!result.Success) { throw new InvalidOperationException(); } var assembly = Assembly.Load(stream.GetBuffer()); } 这个答案建议将ModuleBuilder对象传递给Emit方法而不是MemoryStream,以加快速度。 我试图遵循这种模式,如下: var compilation = CSharpCompilation.Create( “UserPayRules.dll”, syntaxTrees, assembliesToAdd); var assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly( new AssemblyName(“ThisAssembly”), AssemblyBuilderAccess.RunAndCollect); var […]

在Roslyn中获取完全限定的元数据名称

我需要获取特定符号的完整CLR名称。 这意味着对于generics类型,我需要在类型上附加`1 , `2等。 现在, ISymbol已经拥有了一个属性MetadataName 。 但是它排除了周围的类型和名称空间,只给出了手头符号的名称。 获取完全限定名称的常用选项,即通过ToDisplayString在这里不太起作用,因为它不会将MetadataName用于其各个部分。 有这样的内置吗? 或者我必须将ContainingSymbol的链连接起来. 在…之间? (这个假设有没有分解?) 编辑:只是注意到如果它是包含在另一种类型中的类型,你需要在各个名称之间加一个+ ,但除此之外,使用. 应该工作,我想。

如何使用Roslyn的OpenSolutionAsync解析所有引用?

我正在尝试使用OpenSolutionAsync打开RoslynLight.sln然后遍历所有项目。 为了我的目的,我需要一个语义模型和解决引用。 通过这个问题和这个问题的结合 ,我得出了这个部分解决方案: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.MSBuild; using System.IO; namespace OpenRoslyn { class Program { static void Main(string[] args) { var msbw = MSBuildWorkspace.Create(); var sln = msbw.OpenSolutionAsync(@”C:\Users\carr27\Documents\GitHub\roslyn\src\RoslynLight.sln”).Result; //var proj = sln.Projects.First(x => x.Name == “CodeAnalysis.Desktop”); var messages = new List(); foreach (var p […]

修改语法树,然后获取更新的语义模型

我正在尝试修改语法树,然后更新语义模型。 这是我到目前为止: var tree = Roslyn.Compilers.CSharp.SyntaxTree.ParseCompilationUnit(code); var compilation = Roslyn.Compilers.CSharp.Compilation.Create( “MyCompilation”, syntaxTrees: new[] { tree }, references: new[] { mscorlib }); var semanticModel = compilation.GetSemanticModel(tree); … var oldStatementNode = (parent as ExpressionStatementSyntax); //some SyntaxNode in the tree var oldExpressionNode = oldStatementNode.Expression; var newExpressionNode = Syntax.ParenthesizedExpression(oldExpressionNode); var newRootNode = tree.GetRoot().ReplaceNode(oldExpressionNode, newExpressionNode); var semanticInfo = semanticModel.GetTypeInfo(newExpressionNode); //throws […]

将对象实例传递给Roslyn ScriptEngine

我正在寻找一个C#脚本引擎,它可以解释C#代码块,同时维护一个上下文。 例如,如果输入它: var a = 1; ,然后a + 3 ,它将输出4 。 我知道MS Roslyn ,确实这样做,但它是一个沙箱(尊重启动它的程序)。 所以,如果我创建一个ScriptEngine实例和一个MyClass实例(只是我的一个arbirary类),我没有选择将my_class的引用my_class给script_engine 。 有可能以某种方式传递该引用吗? 我想做的是: ScriptEngine engine; // A Roslyn object Session session // A Roslyn object MyClass my_class; // My object // all required initializations Submission sm = session.CompileSubmission(“var a=1;”); dynamic result = sm.Execute(); Submission sm = session.CompileSubmission(“a + 3;”); dynamic […]

未找到类型或命名空间“您缺少程序集引用”,而所有引用都是正确的

我正在尝试使用MSBuildWorkspace类 。 我的项目中包含所有程序集引用。 当我在对象浏览器中打开引用时,我看到了命名空间和我尝试使用的类。 但在我的以下使用声明中, using Microsoft.CodeAnalysis.MSBuild 我得到了一个 The type or namespace name ‘MSBuild’ does not exist in the namespace ‘Microsoft.CodeAnalysis’ (are you missing an assembly reference?) 但有趣的是,Syntax荧光笔识别出类型名称,它的编译器抱怨 这是构建日志 1>c:\windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(1605,5): warning MSB3258: The primary reference “Microsoft.CodeAnalysis.Workspaces” could not be resolved because it has an indirect dependency on the .NET Framework assembly “Microsoft.Build, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a” […]