I'm trying to prototype some code in everyone's favorite prototyping IDE (LINQPad). I want to run a class I've created in the script in a separate AppDomain. For example:
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()
{
}
}
I get the following exception: 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)
Is it possible to do this in LINQPad?
Answering my own question, even though it is from a tweet from 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.
User contributions licensed under CC BY-SA 3.0