Skip to content

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.

Example with action

var codeGenerator = new AppDomainCodeGenerator(); 
codeGenerator.GenerateCode(@"path/to/an/Assembly", (assembly, extraData) =>
{
	// extract information from assembly and generate code here
});

Example with your own class

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());

Change application base

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 };

Include extra data

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);
Clone this wiki locally