public void FuncCompilerTest() { string code = @" using System; public class FuncProvider { public Func<bool> GetFunc() { return (Func<bool>)(() => $Code$); } }"; CompilerResults results = AdHocCSharpCompiler.CompileSource(code.Replace("$Code$", "true"), "TestAssembly"); Func <bool> o = (Func <bool>)results.CompiledAssembly.GetType("FuncProvider").Construct().Invoke("GetFunc"); Expect.IsTrue(o()); }
public GeneratedAssemblyInfo GenerateAssembly() { OnAssemblyGenerating(new ProxyAssemblyGenerationEventArgs { ServiceType = ServiceType, ServiceSettings = ServiceSettings }); ProxyModel proxyModel = RenderCode(); CompilerResults compileResult = AdHocCSharpCompiler.CompileSource(Code.ToString(), FileName, proxyModel.ReferenceAssemblies); if (compileResult.Errors.Count > 0) { throw new CompilationException(compileResult); } GeneratedAssemblyInfo result = new GeneratedAssemblyInfo(FileName, compileResult); result.Save(); OnAssemblyGenerated(new ProxyAssemblyGenerationEventArgs { ServiceType = ServiceType, ServiceSettings = ServiceSettings }); return(result); }
public Assembly GetAssembly(string nameSpace = null) { List <DynamicTypeDescriptor> types = new List <DynamicTypeDescriptor>(); DynamicNamespaceDescriptor ns = null; if (nameSpace != null) { ns = GetNamespaceDescriptor(nameSpace); } else { ns = DynamicTypeDataRepository.GetOneDynamicNamespaceDescriptorWhere(d => d.Namespace == DynamicNamespaceDescriptor.DefaultNamespace); } types = DynamicTypeDataRepository.DynamicTypeDescriptorsWhere(t => t.DynamicNamespaceDescriptorId == ns.Id).ToList(); StringBuilder src = new StringBuilder(); foreach (DynamicTypeDescriptor typeDescriptor in types) { DtoModel dto = new DtoModel ( ns.Namespace, GetClrTypeName(typeDescriptor.TypeName), typeDescriptor.Properties.Select(p => new DtoPropertyModel { PropertyName = GetClrPropertyName(p.PropertyName), PropertyType = GetClrTypeName(p.PropertyType) }).ToArray() ); src.AppendLine(dto.Render()); } CompilerResults results = AdHocCSharpCompiler.CompileSource(src.ToString(), $"{ns.Namespace}.dll"); if (results.Errors.Count > 0) { throw new CompilationException(results); } return(results.CompiledAssembly); }