在单独的 AppDomain 中创建并执行 LINQPad class/method

Create and execute a LINQPad class/method in separate AppDomain

我正在尝试在每个人最喜欢的原型制作中制作一些代码原型 IDE (LINQPad)。我想 运行 class 我在脚本中创建的一个单独的 AppDomain。例如:

void Main()
{
    AppDomain domain = AppDomain.CreateDomain("MyDomain");
    var qry = (AdHocQuery)domain.CreateInstanceAndUnwrap( typeof( AdHocQuery ).Assembly.Location, typeof( AdHocQuery ).FullName );
    qry.Run();

    AppDomain.Unload(domain);
}

class AdHocQuery
{
    public void Run()
    {
    }
}

我得到以下异常:FileLoadException: Could not load file or assembly 'C:\Users\terry.aney\AppData\Local\Temp\LINQPad5\_jpomhdlf\query_onuisp.dll' or one of its dependencies. The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)

是否可以在 LINQPad 中执行此操作?

回答我自己的问题,即使它来自 Joe Albahari 的推文:)

Given that you're specifying assembly location rather than name, use CreateInstanceFromAndUnwrap. Also, use Util.CreateAppDomain instead of AppDomain.CreateDomain to make dependencies resolve. Add Util.NewProcess=true to query if you get type compatibility errors.