CSharpUtils.Templates.Templates.TemplateCodeGenRoslyn.GetTemplateCodeTypeByCode C# (CSharp) Method

GetTemplateCodeTypeByCode() protected method

protected GetTemplateCodeTypeByCode ( string Code ) : Type
Code string
return System.Type
		protected override Type GetTemplateCodeTypeByCode(string Code)
		{
			var tree = SyntaxTree.ParseCompilationUnit(Code);

			var compilation = Compilation.Create(
				"calc.dll",
				options: new CompilationOptions(assemblyKind: AssemblyKind.DynamicallyLinkedLibrary),
				syntaxTrees: new[] { tree },
				references: new[] {
					new AssemblyFileReference(typeof(object).Assembly.Location),
					new AssemblyFileReference(System.Reflection.Assembly.GetAssembly(typeof(TemplateCodeGen)).Location)
				}
			);

			var Errors = String.Join("\n", compilation.GetDiagnostics().Select(Diagnostic => Diagnostic.ToString()));
			if (Errors.Length > 0) throw (new Exception(Errors));

			Assembly compiledAssembly;
			using (var stream = new MemoryStream())
			{
				EmitResult compileResult = compilation.Emit(stream);
				compiledAssembly = Assembly.Load(stream.GetBuffer());
			}

			return compiledAssembly.GetType("CompiledTemplate_TempTemplate");
		}
	}