string CompileAndGetOutput(string fileContent)
{
TempFileCollection tf = new TempFileCollection ();
string path = Path.Combine(tf.BasePath, tf.TempDir);
Directory.CreateDirectory(path);
string generatedScript = Path.Combine(path, "InternalGeneratedScript.cs");
string generatedDLL = Path.Combine(path, "A.DLL");
tf.AddFile(generatedScript, false);
tf.AddFile(generatedDLL, false);
StreamWriter sw = new StreamWriter(generatedScript);
sw.Write(fileContent);
sw.Close();
string output = String.Empty;
string error = String.Empty;
Executor.ExecWaitWithCapture(GetCompilerName() + " /target:library \"/out:" + generatedDLL + "\" \"" + generatedScript +"\"", tf, ref output, ref error);
if (!File.Exists(generatedDLL)) {
StreamReader sr = File.OpenText(output);
string errorMessage = sr.ReadToEnd();
sr.Close();
MessageService.ShowMessage(errorMessage);
return ">>>>ERROR IN CODE GENERATION GENERATED SCRIPT WAS:\n" + fileContent + "\n>>>>END";
}
Assembly asm = Assembly.Load(GetBytes(generatedDLL));
object templateInstance = asm.CreateInstance("Template");
foreach (TemplateProperty property in item.Properties) {
FieldInfo fieldInfo = templateInstance.GetType().GetField(property.Name);
fieldInfo.SetValue(templateInstance, Convert.ChangeType(StringParser.Properties["Properties." + property.Name], property.Type.StartsWith("Types:") ? typeof(string): Type.GetType(property.Type)));
}
MethodInfo methodInfo = templateInstance.GetType().GetMethod("GenerateOutput");
string ret = methodInfo.Invoke(templateInstance, null).ToString();
tf.Delete();
return ret;
}