【问题标题】:Dynamic in memory compilation returns false动态内存编译返回 false
【发布时间】:2017-12-18 17:32:34
【问题描述】:

我有一个动态程序集的编译。我传入一个类型。将其上的属性显式设置为 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; }
}

【问题讨论】:

  • 你到底为什么要这样做?当然有更好的方法可以在不使用动态编译的情况下实现你想要实现的目标.

标签: c# scripting .net-assembly roslyn


【解决方案1】:

鉴于您没有提供对 AVPMVCxFormLayoutItem 类或 AVPControls 参考的详细信息的深入了解,我使用以下内容重新创建了该场景:

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

我无法重现您的问题。

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

【讨论】:

  • 哦.. 我试图传入一个稍微修改过的 (MVCxFormLayoutItem) documentation.devexpress.com/AspNet/…
  • 嘎!!!! Nope.Doesnt 工作,如果参数的缘故:主机有一个类属性 AVPMVCxFormLayoutItem 有一个属性 Complete
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-10
  • 1970-01-01
  • 1970-01-01
  • 2018-09-30
  • 1970-01-01
  • 1970-01-01
  • 2019-05-29
相关资源
最近更新 更多