我在运行时创建类时遇到问题。每次我调试下面的代码时,我都会在var cls = results.CompiledAssembly.GetType("test.DummyHelloWorldHandler");
无法加载文件或程序集 'file:///C:\Users\MyName\AppData\Local\Temp\1ivc3qic.dll' 或其依赖项之一处收到以下错误消息。Das System kann die angegebene Datei nicht finden。
每次我调试程序时,* .dll 文件的名称都不同
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.Reflection;
namespace DynamicNS
{
class Program
{
static void Main(string[] args)
{
CSharpCodeProvider provider = new CSharpCodeProvider();
CompilerParameters parameters = new CompilerParameters();
parameters.GenerateInMemory = true;
parameters.ReferencedAssemblies.Add("System.Collections.dll");
CompilerResults results = provider.CompileAssemblyFromSource(parameters, GetCode());
var cls = results.CompiledAssembly.GetType("test.DummyHelloWorldHandler");
var method = cls.GetMethod("Received", BindingFlags.Static | BindingFlags.Public);
object[] parms = { "Hallo Welt" };
method.Invoke(null, parms);
Console.ReadLine();
}
static string[] GetCode()
{
return new string[]
{
@"using System.Collections;
namespace test
{
public class DummyHelloWorldHandler
{
protected internal Queue _queue;
public void Received(string message)
{
lock (_queue)
{
_queue.Enqueue(message);
}
Console.WriteLine('Enqueued');
}
public DummyHelloWorldHandler()
{
_queue = new Queue();
}
}
}"
};
}
}
}