Visitor.NET 4.0.0

dotnet add package Visitor.NET --version 4.0.0
NuGet\Install-Package Visitor.NET -Version 4.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Visitor.NET" Version="4.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Visitor.NET --version 4.0.0
#r "nuget: Visitor.NET, 4.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Visitor.NET as a Cake Addin
#addin nuget:?package=Visitor.NET&version=4.0.0

// Install Visitor.NET as a Cake Tool
#tool nuget:?package=Visitor.NET&version=4.0.0


First-ever acyclic generic extensible typesafe implementation of Visitor pattern for .NET without any usage of dynamic cast.

With Visitor.NET you can develop typesafe acyclic visitors even if you do not have access to source code of visitable structures.



Install package :


  • Clone locally this github repository
  • Build the Visitor.NET.sln solution

Projects using Visitor.NET


Basic Example

Let's say we have some expression-tree-like hierarchy, implementing basic arithmetics, like this:

public abstract record BinaryTreeNode;

public record Operation(char Symbol, BinaryTreeNode Left, BinaryTreeNode Right) : BinaryTreeNode;

public record Number(double Value) : BinaryTreeNode;

public record Parenthesis(BinaryTreeNode Node) : BinaryTreeNode;

So we may want to traverse it in order to, for example, compute expression result.

First of all, we implement evaluator using IVisitor<,> interface:

public class BinaryTreeEvaluator : VisitorBase<BinaryTreeNode, double>,
    IVisitor<Operation, double>,
    IVisitor<Number, double>,
    IVisitor<Parenthesis, double>
    public double Visit(Operation visitable) =>
        visitable.Symbol switch
            '+' => visitable.Left.Accept(This) + visitable.Right.Accept(This),
            _ => throw new NotImplementedException()

    public double Visit(Number visitable) => visitable.Value;

    public double Visit(Parenthesis visitable) => visitable.Node.Accept(This);

But then we have to tell structures we visit that they are visitable.

It is done through IVisitable<> interface implementation:

public abstract record BinaryTreeNode : IVisitable<BinaryTreeNode>
    public abstract TReturn Accept<TReturn>(
        IVisitor<BinaryTreeNode, TReturn> visitor);

public record Operation(
    char Symbol,
    BinaryTreeNode Left,
    BinaryTreeNode Right) : BinaryTreeNode, IVisitable<Operation>
    public override TReturn Accept<TReturn>(
        IVisitor<BinaryTreeNode, TReturn> visitor) =>

    public TReturn Accept<TReturn>(
        IVisitor<Operation, TReturn> visitor) =>

public record Number(double Value) : BinaryTreeNode, IVisitable<Number>
    public override TReturn Accept<TReturn>(
        IVisitor<BinaryTreeNode, TReturn> visitor) =>

    public TReturn Accept<TReturn>(
        IVisitor<Number, TReturn> visitor) =>

public record Parenthesis(BinaryTreeNode Node) : BinaryTreeNode, IVisitable<Parenthesis>
    public override TReturn Accept<TReturn>(
        IVisitor<BinaryTreeNode, TReturn> visitor) =>

    public TReturn Accept<TReturn>(
        IVisitor<Parenthesis, TReturn> visitor) =>

Basically, if you have access to source code of structure you want "visit", it's better to always have implementation:

return visitor.Visit(this);

In case you would make Visit implementation procedure (i.e. have no returning value), use VisitUnit type as return type.

So, your method would look like this:

public class SomeVisitor : IVisitor<Some>
    public VisitUnit Visit(Some visitable)
        return default;

Adapter Usage

Let's imagine you want to visit some structure defined outside of your project (library, dto, etc.):

public record LinkedListNode<T>(T Data, LinkedListNode<T> Next)
    public bool HasNext() => Next != null;

So we may define wrapper around instance of this type which would became visitable:

public class LinkedListToVisitableAdapter<T> :
    public LinkedListToVisitableAdapter(LinkedListNode<T> data) :

    public override TReturn Accept<TReturn>(
        IVisitor<VisitableAdapter<LinkedListNode<T>>, TReturn> visitor) =>

    public TReturn Accept<TReturn>(
        IVisitor<LinkedListToVisitableAdapter<T>, TReturn> visitor) =>

This adapter can be instantiated with VisitableAdapterFactory<> implementation:

public class LinkedListToVisitableAdapterFactory<T> :
    public override LinkedListToVisitableAdapter<T> Create(LinkedListNode<T> data) =>

Bringing it all together:

public class LinkedListNodePrinter<T> : VisitorNoReturnBase<VisitableAdapter<LinkedListNode<T>>>,
    private readonly StringBuilder _sb = new();
    private readonly VisitableAdapterFactory<LinkedListNode<T>> _factory;

    public LinkedListNodePrinter(VisitableAdapterFactory<LinkedListNode<T>> factory) =>
        _factory = factory;

    public VisitUnit Visit(LinkedListToVisitableAdapter<T> visitable)
        var node = visitable.Data;
        if (node.HasNext())
            var next = _factory.Create(node.Next);

        return default;

    public override string ToString() => _sb.ToString();
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
4.0.0 121 12/11/2023
3.0.0 178 5/2/2023
2.0.0 503 8/22/2022
1.0.0 503 7/28/2022