内存编译中的动态返回false

时间:2017-12-18 17:32:35

标签: c# scripting .net-assembly roslyn

我有动态程序集的编译。我传了一个类型。明确将其属性设置为true。但是在返回的对象(obj1)上,属性(Complete)仍然是false。我如何获得正确的价值?

SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(@"
            using System;
            using System.Windows.Forms;
            using System.Diagnostics;
            using DynamicFormDemo;
            using DynamicFormDemo.Controllers;

            namespace InMemoryCompiledAssembly
            {
                public class Control
                {
                    public static Host ProcessCompleteScript(Host host)
                    {/*
                        if ((Ctrl.FieldValue.ToString() != string.Empty) && (Ctrl.FieldValue != null))
                        {
                            Ctrl.Complete = true; 
                        }
                        else 
                        {
                            Ctrl.Complete = false; 
                        }   */
                       // var t = host.Control;
                        host.Control.Complete = true;
                        MessageBox.Show(host.Control.Complete.ToString());
                        return host;
                    }
                }
            }");
string assemblyName = Path.GetRandomFileName();
MetadataReference[] references = new MetadataReference[]
{
    MetadataReference.CreateFromFile(typeof(object).Assembly.Location),
    MetadataReference.CreateFromFile(typeof(Enumerable).Assembly.Location),
    MetadataReference.CreateFromFile(typeof(Debug).Assembly.Location),
    MetadataReference.CreateFromFile(typeof(MessageBox).Assembly.Location),
    MetadataReference.CreateFromFile(typeof(Host).Assembly.Location),

};

CSharpCompilation compilation = CSharpCompilation.Create(
    assemblyName,
    syntaxTrees: new[] { syntaxTree },
    references: references,
    options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));

Assembly assembly = null;
using (var ms = new MemoryStream())
{
    EmitResult result = compilation.Emit(ms);

    if (!result.Success)
    {
        IEnumerable<Diagnostic> failures = result.Diagnostics.Where(diagnostic =>
            diagnostic.IsWarningAsError ||
            diagnostic.Severity == DiagnosticSeverity.Error);

        foreach (Diagnostic diagnostic in failures)
        {
            Console.Error.WriteLine("{0}: {1}", diagnostic.Id, diagnostic.GetMessage());
        }
    }
    else
    {
        ms.Seek(0, SeekOrigin.Begin);
        assembly = Assembly.Load(ms.ToArray());
    }
}

Host h = new Host();
h.Control = AVPControls[x];

Type type = assembly.GetType("InMemoryCompiledAssembly.Control");
object obj = Activator.CreateInstance(type);
Object obj1 = type.InvokeMember("ProcessCompleteScript",
    BindingFlags.Default | BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.Public,
    null,
    obj,
    new object[] { h });


public class Host
{
    public IAVPBaseControl Control { get; set; }
}

1 个答案:

答案 0 :(得分:0)

鉴于您没有深入了解AVPMVCxFormLayoutItem类或AVPControls引用的详细信息,我使用以下内容重新创建了方案:

public sealed class AVPMVCxFormLayoutItem
{
    public bool Complete { get; set; }
}

我无法重现您的问题。

Complete is true

这让我相信你的问题在于AVPMVCxFormLayoutItem类本身。