roslyn

Using Roslyn Emit method with a ModuleBuilder instead of a MemoryStream

为君一笑 提交于 2019-11-29 05:51:31
I was having trouble with performance when using Roslyn to compile to a dynamic assembly. Compilation was taking ~3 seconds, compared to ~300 milliseconds to compile the same code when using the CodeDom compiler. Here's a pared-down version of the code I'm using to do the compilation: 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

params overload apparent ambiguity - still compiles and works?

我只是一个虾纸丫 提交于 2019-11-29 05:39:38
问题 We just found these in our code: public static class ObjectContextExtensions { public static T Find<T>(this ObjectSet<T> set, int id, params Expression<Func<T, object>>[] includes) where T : class { ... } public static T Find<T>(this ObjectSet<T> set, int id, params string[] includes) where T : class { ... } } As you can see, these have the same signature except for the params . And they're being used in several ways, one of them: DBContext.Users.Find(userid.Value); //userid being an int?

Getting debugger context in C# interactive

戏子无情 提交于 2019-11-29 04:49:09
问题 C# Interactive seems a lot more powerful than the Immediate Window (at least it handles lambda expressions that are often used in LINQ - see Visual Studio debugging "quick watch" tool and lambda expressions), but it looks like it can't be used as a replacement as it doesn't know about the debugger context. Is there a way to have access to the debugger context? I've seen Getting debugger context in F# interactive and it might require the same things, but maybe there are new/different things

Was C# compiler written in C++?

时光怂恿深爱的人放手 提交于 2019-11-29 04:39:30
问题 Was C# compiler written in C++? 回答1: Yes, but there are plans to write a C# compiler in C#, which I believe was discussed in this podcast. 回答2: Yes. The Mono C# compiler is written in C#. 回答3: The .NET framework was written in Simple Managed C (SMC) History During the development of the .NET Framework, the class libraries were originally written using a managed code compiler system called Simple Managed C (SMC).In January 1999, Anders Hejlsberg formed a team to build a new language at the

Dynamic invoke of a method using named parameters

北战南征 提交于 2019-11-29 02:45:24
We're currently using .NET 3.5 and part of our application uses dynamic invocation (using MethodBase.Invoke ) I am wondering if it is possible to mix in Named Parameters (in .NET 4) with dynamic invocation, to perform something similar to: // Dictionary that holds parameter name --> object mapping var parameters = new Dictionary<string, object>(); // Add parameters .... // Invoke where each parameter will match the one from the method signature. methodInfo.Invoke(obj, parameters); Is there any API that allows this option out of the box? If not, is it possible to develop some solution to

Compile-time source code modification using Roslyn

南笙酒味 提交于 2019-11-29 02:31:57
问题 Is it possible to modify source code before compilation using Roslyn within MSBuild task on CI server? I've succeeded to do what I want in VS but I wonder if it is possible outside VS. Currently I'm looking at Workspace APIs and Compiler APIs and they seem to be the right tool to achieve that, but I'm still not sure is it possible at all? In particular I'm concerned about returning changes that I've done to MSBuild back to allow it to continue its job. 回答1: This is definitely a scenario that

Bitwise-or operator used on a sign-extended operand in Visual Studio 2015

那年仲夏 提交于 2019-11-29 01:41:26
I just tried installing Visual Studio 2015, and when trying to compile an old project, I got the warning CS0675 Bitwise-or operator used on a sign-extended operand; consider casting to a smaller unsigned type first for a piece of code that does not give the same warning when compiling in Visual Studio 2013. I found out that all it takes to reproduce is this very simple code: short a = 0; int b = 0; a |= (short)b; Now, I have read this SO question , I have read Eric Lippert's blog post on this issue, and I quickly read up on sign extension , but my understanding is that sign extension happens

MSBuildWorkspace.Create() throws exception

时光总嘲笑我的痴心妄想 提交于 2019-11-29 01:27:21
I have Visual Studio 2013. I also have installed MSBuild Tools 2013. The following code gives me exception var workspace=MSBuildWorkspace.Create(); Here is the exception Could not load file or assembly 'Microsoft.Build, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified. What am I doing wrong ? You need to install the BuildTools for Visual Studio 2015 . You could compile Roslyn against an older version of MSBuild to avoid this problem. I've done this with VS 2012: Src/Workspaces/Core/Workspaces.csproj -

Is it possible to compile a single C# code file with the .NET Core Roslyn compiler?

若如初见. 提交于 2019-11-29 00:10:02
问题 In old .NET we used to be able to run the csc compiler to compile a single .cs file or several files. With .NET Core we have dotnet build that insists on having a proper project file. Is there a stand-alone command line compiler that would allow to compile source code files without having a project (and listing referenced dependencies on the same command line)? On Linux, when I have the old csc and the new .NET Core installed, I get these timings: [root@li1742-80 test]# time dotnet build

C# 7.3 Enum constraint: Why can't I use the nullable enum?

情到浓时终转凉″ 提交于 2019-11-28 22:04:58
问题 Now that we have enum constraint, why doesn't compiler allow me to write this code? public static TResult? ToEnum<TResult>(this String value, TResult? defaultValue) where TResult : Enum { return String.IsNullOrEmpty(value) ? defaultValue : (TResult?)Enum.Parse(typeof(TResult), value); } The compiler says: Error CS0453 The type 'TResult' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'Nullable' 回答1: You can, but you have to add another