동적 어셈블리를 컴파일했습니다. 나는 타입을 건네 줬어. 명시 적으로 속성을 true로 설정합니다. 그러나 반환 된 개체 (obj1) 속성 (완료) 여전히 false입니다. 올바른 값을 얻으려면 어떻게해야합니까?동적 메모리 컴파일은 false를 반환합니다.
public sealed class AVPMVCxFormLayoutItem
{
public bool Complete { get; set; }
}
내가 당신의 문제를 재현 할 수없는입니다 :
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; }
}
*** 왜이 지구상에 있습니까? *** 동적 컴파일을 사용하지 않고 달성하려는 목표를 달성하는 더 좋은 방법이 있습니다. –