roslyn

Getting interface implementations in referenced assemblies with Roslyn

倾然丶 夕夏残阳落幕 提交于 2019-12-20 10:36:11
问题 I'd like to bypass some classical assembly scanning techniques in a framework I am developing. So, say I've defined the following contract: public interface IModule { } This exists in say Contracts.dll . Now, if I want to discover all implementations of this interface, we would probably do something similar to the following: public IEnumerable<IModule> DiscoverModules() { var contractType = typeof(IModule); var assemblies = AppDomain.Current.GetAssemblies() // Bad but will do var types =

Loading a Roslyn compiled assembly into a sandbox AppDomain

坚强是说给别人听的谎言 提交于 2019-12-20 10:25:41
问题 I've got a code-snippet that compiles a script with the script engine and I retreiv the assembly as a byte array. Now I want to load this Assembly in a Sandbox, this is what I've got: Assembly _dynamicAssembly; ScriptEngine _engine; Session _session; public string Execute(string code) { // Setup sandbox var e = new Evidence(); e.AddHostEvidence(new Zone(SecurityZone.Internet)); var ps = SecurityManager.GetStandardSandbox(e); var setup = new AppDomainSetup { ApplicationBase = Environment

Null propagation operator and extension methods

倾然丶 夕夏残阳落幕 提交于 2019-12-20 10:18:32
问题 I've been looking at Visual Studio 14 CTP along with C# 6.0 and playing with the null-propagation operator. However, I couldn't find why the following code does not compile. The features are not yet documented so I'm not sure whether this is a bug or extension methods simply are not supported with the ?. operator and the error message is misleading. class C { public object Get() { return null; } } class CC { } static class CCExtensions { public static object Get(this CC c) { return null; } }

Roslyn, MSBuildWorkspace compiling a .NetStandard project referencing a .NetStandard project throws “No value for RuntimeMetadataVersion found.”

徘徊边缘 提交于 2019-12-20 05:23:51
问题 Create a solution with the following projects. A .NetStandard2.0 project, name it "1" A .NetStandard2.0 project, name it "2" Add a reference of project 1 in 2 A .Net4.6.1 console project name it "3" and add the nuget Microsoft.CodeAnalysis.CSharp.Workspaces v2.7.0 Paste in the follwing code into project 3 static void Main(string[] args) { string dir = Path.Combine(Environment.CurrentDirectory, "..\\..\\..\\"); string solutionPath = Directory.GetFiles(dir, "*.sln").First(); var msWorkspace =

Roslyn compiler version used for projects missing Microsoft.Net.Compilers

跟風遠走 提交于 2019-12-20 03:53:49
问题 Solution consists of 2 dll libraries, 1 console app and 1 web app. Only the last project, the web one, uses Microsoft.Net.Compilers NuGet package. Package has been updated to version 2.3.0 now, what means C# 7.1 and VB 15.3. source: Roslyn NuGet packages As 3 other projects do not have Microsoft.Net.Compilers NuGet package installed, what Roslyn compiler and C# version do they use? I use VS.Net 2017 15.2 with .NET Framework 4.7, so I would guess it shall C#7.0, but still Microsoft.Net

How to specify the equivalent of /features:strict (of csc.exe) to msbuild.exe or in the .csproj file?

旧街凉风 提交于 2019-12-19 19:33:20
问题 Introduction Consider this simple (and bad) C# class: using System; namespace N { static class C { static void M(DateTime d) { if (d == null) Console.WriteLine("Yes"); else Console.WriteLine("No"); } static void L(object o) { if (o is Nullable) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } } Both methods M and L have serious issues. In M , we ask if a value of the non-nullable struct DateTime is equal to null via the lifted == operator (which exists since DateTime overloads

Strange diagnostics errors, prefedined type System… is not defined or imported

时光总嘲笑我的痴心妄想 提交于 2019-12-19 11:21:22
问题 I am getting Roslyn diagnostics errors when parsing a very basic .NET 4.6 application. The solution files can be downloaded from there https://github.com/dotnet/roslyn/files/2393288/DemoSolution.zip The dependency tree looks like this: BLL -> DB I am getting the following diagnostics errors in the BLL project: The solution and projects build fine, still Roslyn gives these errors. Maybe the errors are misleading and I need to configure the projects in someway? Any idea how I can resolve these

How can I get the fully qualified namespace from a using directive in Roslyn?

笑着哭i 提交于 2019-12-19 10:09:08
问题 When you hover over a "simplified" using directive in VS2015, it shows you the fully-qualified name. How would I get this information via a Roslyn plugin? Would it be using a DiagnosticAnalyzer ? A CodeFixProvider ? Reading through source.roslyn.codeplex.com, there's tons of information there, including how to add a using statement, and also how to simplify type names (including using statements), but I'm unable to figure out how to go in reverse to get the fully-qualified name. 回答1: With the

EventSourceException: No Free Buffers available from the operating system

末鹿安然 提交于 2019-12-19 09:09:52
问题 The full exception text is: EventSourceException: No Free Buffers available from the operating system (e.g. event rate too fast). I am calling this method approx a million times because of recursion. It does not stop, I just get the exception text in the Output Debug windown in VS2013. But it is super slow. private static IEnumerable<string> RecursiveFindServices(ISymbol sym, Solution sln) { List<string> list = new List<string>(); var callers = SymbolFinder.FindCallersAsync(sym, sln).Result;

Roslyn / Find References - Can't properly load Workspace

守給你的承諾、 提交于 2019-12-19 06:44:22
问题 I'm trying to write some code to find all method invocations of any given method as I am looking to create an open source UML Sequence Diagramming tool. I'm having trouble, however, getting past the first few lines of code :/ The API appears to have changed drastically and I can't seem to infer proper usage by looking at the code. When I do: var workspace = new CustomWorkspace(); string solutionPath = @"C:\Workspace\RoslynTest\RoslynTest.sln"; var solution = workspace.CurrentSolution; I find