private static IDictionary <Type, MethodInfo> NewDispatchCache(IDomNodeVisitor v) { var dispatch = new Dictionary <Type, MethodInfo>(); foreach (var face in v.GetType().GetInterfaces()) { if (!typeof(IDomNodeVisitor).IsAssignableFrom(face)) { continue; } if (typeof(IDomNodeVisitor) == face) { continue; } var mapping = v.GetType().GetInterfaceMap(face); for (int i = 0; i < mapping.InterfaceMethods.Length; i++) { if (mapping.InterfaceMethods[i].Name == "Visit") { var elementType = mapping.InterfaceMethods[i].GetParameters()[0].ParameterType; dispatch[elementType] = mapping.TargetMethods[i]; } } } return(dispatch); }
public DomNodeVisitDispatcher(IDomNodeVisitor visitor) { if (visitor == null) { throw new ArgumentNullException(nameof(visitor)); } _visitor = visitor; }
public static void Visit(DomObject obj, IDomNodeVisitor visitor) { if (obj == null) { throw new ArgumentNullException(nameof(obj)); } if (visitor == null) { throw new ArgumentNullException(nameof(visitor)); } DomNodeVisitDispatcher.Create(visitor).Dispatch(obj); }
private static void FastDispatch(IDomNodeVisitor v, DomObject obj) { switch (obj) { case DomElement _: v.Visit((DomElement)obj); return; case DomAttribute _: v.Visit((DomAttribute)obj); return; case DomText _: v.Visit((DomText)obj); return; case DomCDataSection _: v.Visit((DomCDataSection)obj); return; case DomEntityReference _: v.Visit((DomEntityReference)obj); return; case DomEntity _: v.Visit((DomEntity)obj); return; case DomProcessingInstruction _: v.Visit((DomProcessingInstruction)obj); return; case DomComment _: v.Visit((DomComment)obj); return; case DomDocument _: v.Visit((DomDocument)obj); return; case DomDocumentType _: v.Visit((DomDocumentType)obj); return; case DomDocumentFragment _: v.Visit((DomDocumentFragment)obj); return; case DomNotation _: v.Visit((DomNotation)obj); return; } }
public static void VisitAll(IEnumerable <DomObject> objs, IDomNodeVisitor visitor) { if (objs == null || visitor == null) { return; } var d = DomNodeVisitDispatcher.Create(visitor); foreach (var obj in objs) { d.Dispatch(obj); } }
public static IDomNodeVisitDispatcher Create(IDomNodeVisitor visitor) { if (visitor is IDomNodeVisitDispatcher d) { return(d); } var result = new DomNodeVisitDispatcher(visitor); if (result.DispatchCache.IsEmpty) { return(new FastDispatcher(visitor)); } return(result); }
public FastDispatcher(IDomNodeVisitor visitor) { _visitor = visitor; }