Usando el Sistema.Dinámica en Roslyn


Modifiqué el ejemplo que viene con la nueva versión de Roslyn que se lanzó ayer para usar dynamic y ExpandoObject, pero estoy recibiendo un error del compilador que no estoy seguro de cómo solucionarlo. El error es:

(7,21): error CS0656: Falta el compilador requerido miembro 'Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create '

¿Todavía no puedes usar dynamics en el nuevo compilador? ¿Cómo puedo arreglar esto? Este es el ejemplo que actualizé:

[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);
}
 86
Author: Rush Frisby, 2014-04-04

4 answers

Creo que debería hacer referencia a la Microsoft.CSharp.dll asamblea

 213
Author: Alberto,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-04-04 14:04:19

Puede obtener este error en un controlador MVC 6 si olvida poner [FromBody] en un método POST.

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

El.El proyecto predeterminado de NETCore ya incluye la referencia Microsoft.CSharp, pero obtienes prácticamente el mismo mensaje.

Con [FromBody] añadido, ahora puede publicar JSON y luego acceder dinámicamente a las propiedades: -)

 6
Author: Simon_Weaver,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-10-30 03:00:25

Es posible que también desee comprobar las propiedades de todas las referencias de su proyecto. Asegúrese de que cualquier referencia está utilizando. NET más reciente que 2.0. Tengo un proyecto que hacía referencia a otro proyecto en la misma solución y tuve que reconstruir la dependencia usando un destino de.NET framework más nuevo.

Ver este post.

Además, no olvides agregar la referencia Microsoft.CSharp a tu proyecto principal como dijo @Alberto.

 3
Author: A.Clymer,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 11:47:16

Para que el código funcione en. Net Core 2.1 tuve que añadir estas referencias en la compilación:

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),            
    }
);
 1
Author: Renzo Ciot,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-08-21 15:01:29