internal object Resolve(string pathText = null, ObtainMode obtainMode = ObtainMode.Auto, Type targetType = null) { var node = this; Reflection.Expressions.IMemberExpression accessor = null; if (!string.IsNullOrWhiteSpace(pathText)) { var expression = Collections.HierarchicalExpression.Parse(pathText); node = this.Find(expression.Path) ?? throw new PluginException($"Not found the PluginTreeNode with '{expression.Path}' path."); accessor = expression.Accessor; } //获取指定路径的目标对象 object target = node.UnwrapValue(obtainMode, targetType == null ? null : new Builders.BuilderSettings(targetType)); if (target != null && accessor != null) { return(Reflection.Expressions.MemberExpressionEvaluator.Default.GetValue(accessor, target)); } return(target); }
public static HierarchicalExpression Parse(string text, int start, int count, Action <string> onError) { if (string.IsNullOrEmpty(text)) { return(null); } if (start < 0 || start >= text.Length) { throw new ArgumentOutOfRangeException(nameof(start)); } if (count < 1) { count = text.Length - start; } else if (count > text.Length - start) { throw new ArgumentOutOfRangeException(nameof(count)); } //创建解析上下文对象 var context = new StateContext(text.AsSpan(start, count)); Reflection.Expressions.IMemberExpression accessor = null; IList <string> segments = null; //状态迁移驱动 for (int i = start; i < start + count; i++) { if (context.State == State.Exit) { var index = context.Character == '@' ? i : i - 1; if ((accessor = Reflection.Expressions.MemberExpressionParser.Parse(text, index, -1, onError)) == null) { return(null); } else { break; } } if (!context.Read()) { continue; } switch (context.State) { case State.None: if (!DoNone(ref context, i, onError)) { return(null); } break; case State.Slash: if (!DoSlash(ref context, i, onError)) { return(null); } break; case State.AnchorCurrent: if (!DoAnchorCurrent(ref context, i, onError)) { return(null); } break; case State.AnchorParent: if (!DoAnchorParent(ref context, i, onError)) { return(null); } break; case State.Segment: if (!DoSegement(ref context, i, segment => { if (segments == null) { segments = new List <string>(); } segments.Add(segment); }, onError)) { return(null); } break; } } if (context.State == State.Segment) { //获取最终的解析结果 var segment = context.Accept(0); if (segment != null && segment.Length > 0) { if (segments == null) { segments = new string[] { segment } } ; else { segments.Add(segment); } } } return(new HierarchicalExpression(context.Anchor, segments?.ToArray(), accessor)); }