在罗斯林使用系统

我修改了昨天发布的新版 Roslyn 的示例,使用了动态对象和 ExpandObject,但是我得到了一个编译器错误,我不知道如何修复。错误是:

(7,21) : error CS0656: 缺少编译器所需的成员‘ Microsoft.CSharp. RuntimeBinder.CSharpArgumentInfo. Create’

你能不能在新的编译器中使用动力学?我该怎么补救?下面是我更新的例子:

[TestMethod]
public void EndToEndCompileAndRun()
{
var text = @"using System.Dynamic;
public class Calculator
{
public static object Evaluate()
{
dynamic x = new ExpandoObject();
x.Result = 42;
return x.Result;
}
}";


var tree = SyntaxFactory.ParseSyntaxTree(text);
var compilation = CSharpCompilation.Create(
"calc.dll",
options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary),
syntaxTrees: new[] {tree},
references: new[] {new MetadataFileReference(typeof (object).Assembly.Location), new MetadataFileReference(typeof (ExpandoObject).Assembly.Location)});


Assembly compiledAssembly;
using (var stream = new MemoryStream())
{
var compileResult = compilation.Emit(stream);
compiledAssembly = Assembly.Load(stream.GetBuffer());
}


Type calculator = compiledAssembly.GetType("Calculator");
MethodInfo evaluate = calculator.GetMethod("Evaluate");
string answer = evaluate.Invoke(null, null).ToString();


Assert.AreEqual("42", answer);
}
28367 次浏览

I think that you should reference the Microsoft.CSharp.dll assembly

You may also want to check the properties of all your project references. Make sure any reference is using .NET newer than 2.0. I have a project that was referencing another project in the same solution and had to rebuild the dependency using a newer .NET framework target.

See this post.

Also, don't forget to add the Microsoft.CSharp reference to you main project like @Alberto said.

ASP.NET MVC specific:

You can get this error in an MVC 6 controller if you forget to put [FromBody] in a POST method.

    [HttpPost("[action]")]
public void RunReport([FromBody]dynamic report)
{
...
}

The .NETCore default project already includes Microsoft.CSharp reference but you get pretty much the same message.

With [FromBody] added you can now post JSON and then just dynamically access the properties :-)

To make the code work in .Net Core 2.1 I had to add this references in the compilation:

var compilation = CSharpCompilation.Create(
"calc.dll",
options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary),
syntaxTrees: new[] {tree},
references: new[] {
MetadataReference.CreateFromFile(typeof(object).Assembly.Location),
MetadataReference.CreateFromFile(typeof(ExpandoObject).Assembly.Location),
MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("Microsoft.CSharp")).Location),
MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("netstandard")).Location),
MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("mscorlib")).Location),
MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("System.Runtime")).Location),
}
);

If your project is targeting .Net Core or .Net Standard, then instead of adding reference you can install the Microsoft.CSharp NuGet package to solve this error.