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