roslyn

.Net Core - CS0012 'Object' is defined in an assembly that is not referenced

允我心安 提交于 2019-12-10 22:47:39
问题 I'm brand new to .Net Core and I'm trying to build a build system based on it. As part of this project, I've created an abstract class that spells out what a build task should implement, and I've stuffed this into a shared library. The executable project references this library and scans project directories for a specially named directory, then checks to see if there are any .cs files in there. These scripts are loaded and then compilation is attempted using the tools provided through

VS2019 Roslyn Compiler Generic Constraint Method Resolution

谁说胖子不能爱 提交于 2019-12-10 22:43:16
问题 We recently found an issue in our code base, where VS2019 Compiled code fine but VS 2017 Failed. I've created an extension method for Union which has a generic ISet as a Generic Constraint using System; using System.Collections.Generic; using System.Linq; public static class Extensions { public static S Union<S, T>(this S self, IEnumerable<T> other) where S : ISet<T>, new() { //For simplicity issues since this is a compilation based question return default(S); } public static void Test() {

Abort roslyn script execution

时光毁灭记忆、已成空白 提交于 2019-12-10 22:20:24
问题 I'd like to implement timeout mechanism for scripts, executed through Roslyn, so I need a way to abort a script execution. The only solution I found so far, is executing the script inside a thread and terminating it, but it's obviously a flawed solution. Is there a better way to do this? 回答1: Other than you launching a separate process and killing that instead (to avoid the standard pitfalls of Thread.Abort() ), that's the best you can do. Given the script code can be more or less arbitrary,

Obtaining name of a method parameter from the corresponding argument in a method invocation in Roslyn

无人久伴 提交于 2019-12-10 21:21:34
问题 I was wondering if there was some existing logic to obtain the name (or any other relevant information) about the definition of a parameter in its containing method/constructor signature by looking at an invocation of that particular method/constructor. Basically, I just want to be able to get a default name for a variable that will be passed as an argument to the invocation. So, if a method if defined as such: public void Foo(object firstParam, object secondParam, object thirdParam) I would

WPF/XAML: How to reference class that is not defined within any namespace

谁说我不能喝 提交于 2019-12-10 20:57:41
问题 I'm executing a roslyn script that tries to define and open a WPF window. Amongst other things, my script defines an attached behavior defines a XAML string, based on which I create a WPF Window. In this XAML code, I'd like to use the TextBoxCursorPositionBehavior defined in my script. my script (.csx) file looks similar to public class TextBoxCursorPositionBehavior : DependencyObject { // see http://stackoverflow.com/questions/28233878/how-to-bind-to-caretindex-aka-curser-position-of-an

Difference between ISymbol.DeclaringSyntaxReferences and ISymbol.Locations

坚强是说给别人听的谎言 提交于 2019-12-10 20:17:40
问题 What is the difference between the DeclaringSyntaxReferences property and Locations property in the ISyntax interface? 回答1: The clue to the answer is in the <remarks> comment section: The syntax node(s) that declared the symbol. If the symbol was declared in metadata or was implicitly declared, returns an empty read-only array. Which means, that Locations also returns metadata reference declarations and implicitly declared locations. You can see evidence for that in the LocationsTests.cs file

Property-of aka infoof — via Rosyln?

大城市里の小女人 提交于 2019-12-10 20:06:35
问题 A few years ago Eric Lippert blogged about a hypothetical infoof operator that would allow you to get method/property information at compile time i stead of at runtime. Basically saying that it's a nice pony, but no unicorn.... I read a bit about Rosyln's ability to rewrite your code at compile time, how difficult would it be to implement infoof using Roslyn? EDIT: I thought this might be a way to get everything Eric said you they were thinking about, but I should have broken down the

How to distinguish version of compiler using preprocessor directive

北战南征 提交于 2019-12-10 19:43:26
问题 Is there a way to distinguish which version of compiler using preprocessor directives? I'm having trouble with the new Roslyn compiler, which generates different code than the old compiler and I need to keep the code compatible for the team, which is using both Visual Studio 2013 and 2015. 来源: https://stackoverflow.com/questions/32987410/how-to-distinguish-version-of-compiler-using-preprocessor-directive

What's the most efficient way to use Roslyn's CompletionSevice when typing?

ε祈祈猫儿з 提交于 2019-12-10 19:39:57
问题 I'm looking at Roslyn's CompletionService, and ShouldTriggerCompletion is defined as public virtual bool ShouldTriggerCompletion( SourceText text, int caretPosition, CompletionTrigger trigger, ImmutableHashSet<string> roles = null, OptionSet options = null ) { … } where CompletionTrigger is a wrapper for a single char Character . This seems to imply I should call ShouldTriggerCompletion on each char when typing — however that would mean I need to update SourceText on each char, which

C# closure heap allocation happening at start of method

蹲街弑〆低调 提交于 2019-12-10 19:11:53
问题 I seem to have run into some odd behavior of the C# compiler. Consider the following code sample: static void Main(string[] args) { Foo(false, 8); } public static void Foo(bool execute, int x) { if (execute) { Task.Run(() => Console.WriteLine(x)); } } Running this (in release) shows some unexpected allocations happening. Examining the IL shows that that the heap allocation triggered by the closure appears at the very start of the function, rather than inside the condition: .method public