public static void GenerateFile(string filePath, StringContextContainer context, string templateContent, string[] assemblyNames = null, EntryConfig config = null) { string result = Generate(context, templateContent, assemblyNames, config); if (!string.IsNullOrEmpty(result)) { File.WriteAllText(filePath, result); } }
public static string Generate(StringContextContainer context, string templateContent, string[] assemblyNames = null, EntryConfig config = null) { config = config ?? EntryConfig.Default; if (string.IsNullOrEmpty(config.ClassName) || string.IsNullOrEmpty(config.MethodName)) { return(null); } string code = ComposeCode(templateContent); if (string.IsNullOrEmpty(code)) { return(null); } if (!string.IsNullOrEmpty(config.CodeOutputPath)) { File.WriteAllText($"{config.CodeOutputPath}/{config.ClassName}.cs", code); } List <string> assemblyList = new List <string>(DefaultAssemblies); if (assemblyNames != null && assemblyNames.Length > 0) { assemblyList.AddRange(assemblyNames); } Assembly assembly = CompileCode(assemblyList.Distinct().ToArray(), code); if (assembly == null) { return(null); } Type type = assembly.GetType(config.ClassName); MethodInfo mInfo = type.GetMethod(config.MethodName, BindingFlags.Static | BindingFlags.Public); object result = mInfo.Invoke(null, new object[] { context }); return(result?.ToString()); }