-
Notifications
You must be signed in to change notification settings - Fork 30
Work inside a different app domain
MilleBo edited this page Feb 26, 2017
·
1 revision
Testura.Code provides helper classes to load an assembly and generate/compile inside a different app domain.
var codeGenerator = new AppDomainCodeGenerator();
codeGenerator.GenerateCode(@"path/to/an/Assembly", (assembly, extraData) =>
{
// extract information from assembly and generate code here
});
Start by inherit from CodeGeneratorProxy
class MyCodeGeneratorProxy : CodeGeneratorProxy
{
protected override void GenerateCode(Assembly assembly, IDictionary<string, object> extraData)
{
// extract information from assembly and generate code here
}
}
Then use AppDomainCodeGenerator again
var codeGenerator = new AppDomainCodeGenerator();
codeGenerator.GenerateCode(@"path/to/an/Assembly", new MyCodeGeneratorProxy());
Sometimes its necessary to change the application base (for example when using it inside an ASP.NET MVC project). You can change it simply like this:
var codeGenerator = new AppDomainCodeGenerator { ApplicationBase = HttpRuntime.BinDirectory };
You can also send extra data to the new app domain, simple populate the extraData dictionary.
var eD = new Dictionary<string, object>
{
["RequiredValue"] = 1
};
var codeGenerator = new AppDomainCodeGenerator();
codeGenerator.GenerateCode(@"path/to/an/Assembly", (assembly, extraData) =>
{
var requiredValued = extraData["RequiredValue"];
// extract information from assembly and generate code here
}, eD);