public bool Load() { bool success = false; try { if (AssemblyName != null) { Assembly = Assembly.Load(AssemblyName); } success = true; } catch (Exception ex) { if (TemplatingEngine.IsCriticalException(ex)) { throw; } } return(success); }
public string Process(object textTransformation) { if (textTransformation == null) { throw new ArgumentNullException(nameof(textTransformation)); } try { var ttType = textTransformation.GetType(); var errorProp = ttType.GetProperty("Errors", BindingFlags.Instance | BindingFlags.NonPublic); if (errorProp == null) { throw new ArgumentException("Template must have 'Errors' property"); } var errorMethod = ttType.GetMethod("Error", new Type[] { typeof(string) }); if (errorMethod == null) { throw new ArgumentException("Template must have 'Error(string message)' method"); } var errors = (CompilerErrorCollection)errorProp.GetValue(textTransformation, null); errors.Clear(); //set the culture if (culture != null) { ToStringHelper.FormatProvider = culture; } else { ToStringHelper.FormatProvider = CultureInfo.InvariantCulture; } string output = null; var initMethod = ttType.GetMethod("Initialize"); var transformMethod = ttType.GetMethod("TransformText"); if (initMethod == null) { errorMethod.Invoke(textTransformation, new object[] { "Error running transform: no method Initialize()" }); } else if (transformMethod == null) { errorMethod.Invoke(textTransformation, new object[] { "Error running transform: no method TransformText()" }); } else { try { initMethod.Invoke(textTransformation, null); output = (string)transformMethod.Invoke(textTransformation, null); } catch (Exception ex) { if (TemplatingEngine.IsCriticalException(ex)) { throw; } errorMethod.Invoke(textTransformation, new object[] { "Error running transform: " + ex }); } } host.LogErrors(errors.ToTemplateErrorCollection()); ToStringHelper.FormatProvider = CultureInfo.InvariantCulture; return(output); } finally { #if FEATURE_APPDOMAINS CurrentDomain.AssemblyResolve -= ResolveReferencedAssemblies; #endif } }