roslyn

[ASP.NET Core MVC] 如何实现运行时动态定义Controller类型?

北城以北 提交于 2020-04-09 00:55:32
昨天有个朋友在微信上问我一个问题:他希望通过动态脚本的形式实现对ASP.NET Core MVC应用的扩展,比如在程序运行过程中上传一段C#脚本将其中定义的Controller类型注册到应用中,问我是否有好解决方案。我当时在外边,回复不太方便,所以只给他说了两个接口/类型:IActionDescriptorProvider和ApplicationPartManager。这是一个挺有意思的问题,所以回家后通过两种方案实现了这个需求。源代码从 这里 下载。 一、实现的效果 我们先来看看实现的效果。如下所示的是一个MVC应用的主页,我们可以在文本框中通过编写C#代码定义一个有效的Controller类型,然后点击“Register”按钮,定义的Controller类型将自动注册到MVC应用中 由于我们采用了针对模板为“{controller}/{action}”的约定路由,所以我们采用路径“/foo/bar”就可以访问上图中定义在FooController中的Action方法Bar,下图证实了这一点。 二、动态编译源代码 要实现如上所示的“针对Controller类型的动态注册”,首先需要解决的是针对提供源代码的动态编译问题,我们知道这个可以利用Roslyn来解决。具体来说,我们定义了如下这个ICompiler接口,它的Compile方法将会对参数sourceCode提供的源代码进行编译

[ASP.NET Core MVC] 如何实现运行时动态定义Controller类型?

与世无争的帅哥 提交于 2020-04-08 21:15:04
原文: [ASP.NET Core MVC] 如何实现运行时动态定义Controller类型? 昨天有个朋友在微信上问我一个问题:他希望通过动态脚本的形式实现对ASP.NET Core MVC应用的扩展,比如在程序运行过程中上传一段C#脚本将其中定义的Controller类型注册到应用中,问我是否有好解决方案。我当时在外边,回复不太方便,所以只给他说了两个接口/类型:IActionDescriptorProvider和ApplicationPartManager。这是一个挺有意思的问题,所以回家后通过两种方案实现了这个需求。源代码从 这里 下载。 一、实现的效果 我们先来看看实现的效果。如下所示的是一个MVC应用的主页,我们可以在文本框中通过编写C#代码定义一个有效的Controller类型,然后点击“Register”按钮,定义的Controller类型将自动注册到MVC应用中 由于我们采用了针对模板为“{controller}/{action}”的约定路由,所以我们采用路径“/foo/bar”就可以访问上图中定义在FooController中的Action方法Bar,下图证实了这一点。 二、动态编译源代码 要实现如上所示的“针对Controller类型的动态注册”,首先需要解决的是针对提供源代码的动态编译问题,我们知道这个可以利用Roslyn来解决。具体来说

Why is C# CS0136 error not reported when compiled with /langversion:latest [duplicate]

拟墨画扇 提交于 2020-03-21 07:00:13
问题 This question already has an answer here : Lambda parameter name (sometimes) conflicting with local name (1 answer) Closed 11 days ago . The code below compiles fine in Debug but failed with a CS0136(A local or parameter named 'x' cannot be decaled..) error in Release. Anybody has any idea why the error is not reported in Debug build? public void test() { Action<int> a = x => x++; int x = 0; } Target framework 4.6.1 VS version: 16.4.3 and 16.4.5 MSBuildVersion: 16.4.0 MSBuildRuntimeVersion =

一起了解 .Net Foundation 项目 No.5

痴心易碎 提交于 2020-02-28 09:21:08
.Net 基金会中包含有很多优秀的项目,今天就和笔者一起了解一下其中的一些优秀作品吧。 中文介绍 中文介绍内容翻译自英文介绍,主要采用意译、如与原文存在出入,请以原文为准。 Couchbase Lite for .NET 该项目是采用 C# 语言基于 Couchbase Lite 的 Java 版本代码进行重制的 Couchbase Lite。 Couchbase Lite 是一个全功能的、嵌入式的、轻量级的、原生良好的文档型 JSON 数据库。 使用 Couchbase Lite ,您可以在轻量型设备上拥有全功能的 Couchbase 体验。您可以进行创建、更新、删除、查询、同步等等一系列操作。 项目详情 官网地址 项目源码 项目许可证: MIT 项目联系人: Zachary Gramana 相关链接 参与讨论 笔者简评 Couchbase 是一款冉冉升起的 NoSQL 数据库产品。 Couchbase Lite 则是其轻量版,主要支持运行在轻量型设备上。 Couchbase Lite for .NET 则是采用 C# 编写的版本,其与 .Net 语言具有更好集成的互操作性。 如果要深入的了解 NoSQL,仅仅只是了解 Mongdb 恐怕是不够的,排名第三的 Couchbase 了解一下? (截至 2020 年 1 月 19 日 17:05:52 ) 英文介绍 Couchbase

好奇的null-coalescing运算符自定义隐式转换行为

夙愿已清 提交于 2020-02-26 22:41:20
注意:这似乎已在 Roslyn中 修复 写我的回答时,此问题出现了 这一个 ,其中谈到了的关联性 空合并运算符 。 提醒一下,null-coalescing运算符的概念就是表单的表达式 x ?? y 首先评估 x ,然后: 如果 x 值为null,则计算 y ,这是表达式的最终结果 如果 x 值为非null, 则不 计算 y ,并且 x 的值是表达式的最终结果,在必要时转换为编译时类型的 y 现在 通常 不需要转换,或者它只是从可空类型到非可空类型 - 通常类型是相同的,或者只是来自(比如说) int? 到 int 。 但是,您 可以 创建自己的隐式转换运算符,并在必要时使用它们。 对于 x ?? y 的简单情况 x ?? y ,我没有看到任何奇怪的行为。 但是,用 (x ?? y) ?? z (x ?? y) ?? z 我看到一些混乱行为。 这是一个简短但完整的测试程序 - 结果在评论中: using System; public struct A { public static implicit operator B(A input) { Console.WriteLine("A to B"); return new B(); } public static implicit operator C(A input) { Console.WriteLine("A to C");

Closure allocations in C#

不想你离开。 提交于 2020-02-19 09:46:41
问题 I've installed the Clr Heap Allocation Analyzer extension and in a project I see something that I quite don't understand, I've got a method with a signature public Task<int> ExecuteAsync(string sql, dynamic param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null) { param = SetModificationValuesForGlobalRing(param); return _sqlPolicy.ExecuteAsync(async () => { int result; using (var connection = new SqlConnection(_connectionString)) { await

Map tokens and trivia to line numbers

两盒软妹~` 提交于 2020-02-02 05:20:26
问题 I'm trying to map tokens and trivia to line numbers using Roslyn. Here's my latest attempt with help from @Kevin Pilch-Bisson below. public class CSharpSlocAnalyser : ISlocAnalyser { public long GetSlocFor(IEnumerable<FileInfo> files, SlocOptions options) { var tree = CSharpSyntaxTree.ParseText( @"using /* Blah */ System; // Blah public class MyClass { public void MyMethod() { var blah = ""abc""; } }"); var root = tree.GetRoot(); var walker = new CustomWalker(); walker.Visit(root); var

Get a list of referenced Types within a project with Roslyn

拥有回忆 提交于 2020-01-24 03:40:27
问题 I want to get a list of all used types in a project, for example: var x = 1; var y = x.ToString().GetType(); The code should return System.Int32 , System.String , System.Type . What I have is freaking slow... for each file (syntax tree), I do the following: var root = await syntaxTree.GetRootAsync(); var nodes = root.DescendantNodes(n => true); if (nodes != null) { var syntaxNodes = nodes as SyntaxNode[] ?? nodes.ToArray(); // IdentifierNameSyntax: // - var keyword // - identifiers of any

Add a parameter to a method with a Roslyn CodeFixProvider

亡梦爱人 提交于 2020-01-23 07:53:13
问题 I'm writing a Roslyn Code Analyzer that I want to identify if an async method does not take a CancellationToken and then suggest a code fix that adds it: //Before Code Fix: public async Task Example(){} //After Code Fix public async Task Example(CancellationToken token){} I've wired up the DiagnosticAnalyzer to correctly report a Diagnostic by inspecting the methodDeclaration.ParameterList.Parameters , but I can't find the Roslyn API for adding a Paramater to the ParameterList inside a

Can I detect document saved (not changed) with Visual Studio Workspace in a VSIX?

偶尔善良 提交于 2020-01-23 02:14:30
问题 In my extension I use to tap into the document saved event using DTE: this._events2 = (Events2)this._dte.Events; //setup document events this._documentEvents = this._events2.DocumentEvents; this._documentEvents.DocumentSaved += _documentEvents_DocumentSaved; I am migrating my extension to VS 2017 and want to start using the Roslyn stuff instead of DTE. I figured out how to get the Visual Studio Workspace and tap into the workspace changed event. Now I have access to all these document events