public static MethodInfo GetMethodFromFullName(this AssemblyCSharpBuilder builder, string typeName, string methodName) { var type = builder.GetTypeFromFullName(typeName); if (type == null) { return(null); } var info = type.GetMethod(methodName); if (info == null) { NatashaException exception = new NatashaException($"无法在类型 {typeName} 中找到该方法 {methodName}!"); if (builder.Exceptions.Count == 0) { exception.ErrorFlag = ExceptionKind.Method; } else { exception.ErrorFlag = ExceptionKind.Assembly; } builder.Exceptions.Add(exception); } return(info); }
/// <summary> /// 拿到异常之后进行后处理,如果处理后可以重编译,将再次编译 /// </summary> /// <param name="errors"></param> /// <returns></returns> private bool CanRetryCompile(ImmutableArray <Diagnostic> errors) { if (CanRetry) { _retryCount += 1; if (_retryCount < RetryLimit) { Dictionary <string, string> codeMappings = new Dictionary <string, string>(); foreach (var item in errors) { if (item.Location.SourceTree != null) { var sourceCode = item.Location.SourceTree.ToString(); if (!codeMappings.ContainsKey(sourceCode)) { codeMappings[sourceCode] = sourceCode; } if (ErrorHandlers.ContainsKey(item.Id)) { codeMappings[sourceCode] = ErrorHandlers[item.Id](item, Syntax, codeMappings[sourceCode]); } } } foreach (var item in codeMappings) { Syntax.Update(item.Key, item.Value); } return(true); } } Exceptions = NatashaException.GetCompileException(Compiler.AssemblyName, errors); return(false); }
public static Type GetTypeFromShortName(this AssemblyCSharpBuilder builder, string typeName) { Assembly assembly = builder.GetAssembly(); if (assembly == null) { return(null); } var type = assembly.GetTypes().First(item => item.Name == typeName); if (type == null) { NatashaException exception = new NatashaException($"无法在程序集 {builder.Compiler.AssemblyName} 中找到该类型 {typeName}!"); if (builder.Exceptions.Count == 0) { exception.ErrorFlag = ExceptionKind.Type; } else { exception.ErrorFlag = ExceptionKind.Assembly; } builder.Exceptions.Add(exception); } return(type); }
public static Delegate GetDelegateFromShortName(this AssemblyCSharpBuilder builder, string typeName, string methodName, Type delegateType, object target = null) { var info = builder.GetMethodFromShortName(typeName, methodName); if (info == null) { return(null); } try { return(info.CreateDelegate(delegateType, target)); } catch (Exception ex) { NatashaException exception = new NatashaException($"在类型 {typeName} 中找到的方法 {methodName} 向 {delegateType.FullName} 转换时出错!"); if (builder.Exceptions.Count == 0) { exception.ErrorFlag = ExceptionKind.Delegate; } else { exception.ErrorFlag = ExceptionKind.Assembly; } builder.Exceptions.Add(exception); } return(null); }
internal static NatashaException GetCompileException(CSharpCompilation compilation, ImmutableArray <Diagnostic> errors) { var first = errors[0]; var exception = new NatashaException(first.GetMessage()); exception.Diagnostics.AddRange(errors); if (first.Location.SourceTree != null) { exception.Formatter = first.Location.SourceTree.ToString(); } exception.CompileMessage = $"编译程序集为:{compilation.AssemblyName};CSharp版本:{compilation.LanguageVersion};"; exception.ErrorKind = NatashaExceptionKind.Compile; return(exception); }
public CompilationException Add(string script, HashSet <string> sets = default) { var tree = AddTreeToCache(script); var exception = NatashaException.GetSyntaxException(tree); if (!exception.HasError) { UsingCache[exception.Formatter] = sets; } else { HandlerErrors(exception); } return(exception); }
/// <summary> /// 错误处理 /// </summary> /// <param name="exception"></param> private void HandlerErrors(NatashaException exception) { if (SyntaxErrorBehavior == ExceptionBehavior.Throw) { throw exception; } else if (SyntaxErrorBehavior == ExceptionBehavior.Log) { LogOperator.ErrorRecoder(exception); } else if (SyntaxErrorBehavior == (ExceptionBehavior.Log | ExceptionBehavior.Throw)) { LogOperator.ErrorRecoder(exception); throw exception; } }
public CompilationException Add(string script, HashSet <string> sets = default) { var tree = Syntax.LoadTreeFromScript(script); var exception = NatashaException.GetSyntaxException(tree); if (!exception.HasError || SyntaxErrorBehavior == ExceptionBehavior.Ignore) { Syntax.AddTreeToCache(tree); Syntax.ReferenceCache[exception.Formatter] = sets; } else { HandlerErrors(exception); } return(exception); }
internal static NatashaException?GetSyntaxException(SyntaxTree tree) { var diagnostics = tree.GetDiagnostics(); var errors = diagnostics.Where(item => !item.IsSuppressed).ToArray(); if (errors.Length > 0) { var first = errors[0]; var exception = new NatashaException(first.GetMessage()); exception.Diagnostics.AddRange(errors); exception.Formatter = tree.ToString(); exception.ErrorKind = NatashaExceptionKind.Syntax; return(exception); } return(null); }
public static void Deconstruct( this string script, out Assembly Assembly, out NatashaException Exception) { AssemblyCSharpBuilder assembly = new AssemblyCSharpBuilder(); assembly.Add(script); Assembly = assembly.GetAssembly(); if (assembly.Exceptions != null) { Exception = assembly.Exceptions[0]; } else { Exception = null; } }
/// <summary> /// 拿到异常之后进行后处理,如果处理后可以重编译,将再次编译 /// </summary> /// <param name="errors"></param> /// <returns></returns> private bool CanRetryCompile(ImmutableArray <Diagnostic> errors) { Exceptions = NatashaException.GetCompileException(Compiler.AssemblyName, errors); Dictionary <string, string> codeMappings = new Dictionary <string, string>(); foreach (var item in Exceptions) { if (item.Formatter != default) { codeMappings[item.Formatter] = item.Formatter; if (item.HasError) { foreach (var error in item.Diagnostics) { if (ErrorHandlers.ContainsKey(error.Id)) { CanRetry = true; ErrorHandlers[error.Id](item, error, Syntax, codeMappings); } } } } } if (CanRetry) { CanRetry = false; RetryCount += 1; if (RetryCount < RetryLimit) { foreach (var item in codeMappings) { Syntax.Update(item.Key, item.Value); } return(true); } } return(false); }