https://github.com/micheledicosmo/staticvisitor
A customizable data structure static visitor for C#
https://github.com/micheledicosmo/staticvisitor
data-structures-algorithms dotnet dotnet-library dotnet-standard dotnet-standard2 nuget-package
Last synced: 5 months ago
JSON representation
A customizable data structure static visitor for C#
- Host: GitHub
- URL: https://github.com/micheledicosmo/staticvisitor
- Owner: micheledicosmo
- License: other
- Created: 2019-10-22T10:33:57.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2021-03-03T18:59:16.000Z (over 4 years ago)
- Last Synced: 2025-04-02T02:22:12.773Z (7 months ago)
- Topics: data-structures-algorithms, dotnet, dotnet-library, dotnet-standard, dotnet-standard2, nuget-package
- Language: C#
- Homepage:
- Size: 64.5 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# StaticVisitor (namespace: Sid.Tools.StaticVisitor)
A customizable data structure static visitor for C#.
This package, released under MIT license, allows to statically visit data structures easily in C#. Static visit means that it's the types (classes) of the data structure that get visited, not the instances.
## Usages
The basic usage is to create an instance of `Sid.Tools.StaticVisitor.StaticVisitor` and pass an `IList>` that will collect the results of the visit, then invoke the `Visit` method.
Overloads for more advanced behaviour also exist, where you specify a custom `Action>` which defines what custom action to execute upon visit.
The easiest way to access the visited type in the returned list or upon invocation of the above Action, is via the `[Stack].CurrentType()` or `[Stack].CurrentVisit()` extension methods, but it is possible to observe the entire stack as well, allowing to understand why a type was visited, and how it was reached.
Note that not all types are visited by default: by default [primitive types](https://docs.microsoft.com/en-us/dotnet/api/system.type.isprimitive) and `object` are not visited; this behaviour can be customized (see below).
Visit behaviour can be customized by passing a custom `StaticVisitorConfiguration` instance.
### Basic example
The method `DoSomething`
```
public class DataStructure {
public Property SomeProperty { get; }
}
public class Property {}
public void DoSomething() {
var visitor = new StaticVisitor(out var list);
visitor.Visit(typeof(DataStructure));
foreach(var stack in list)
Console.WriteLine(stack.CurrentType())
}
```
will write the following types to the console:
- `DataStructure`
- `Property`
### Custom type filtering example
The following code
```
var visitor = new Sid.Tools.StaticVisitor.StaticVisitor(out var list, new Tools.DataStructure.StaticVisitorConfiguration()
{
TypeCanBeVisited = x =>
Sid.Tools.StaticVisitor.StaticVisitorConfiguration.DefaultTypeCanBeVisited(x)
&& !x.IsValueType
});
```
will specify which types should be visited and which shouldn't.
Specifically it will preserve the default behaviour, but also exclude types that are value types.
### Advanced configuration
Please refer to the docstring documentation in https://github.com/micheledicosmo/StaticVisitor/blob/master/StaticVisitor/StaticVisitor.cs in the object `StaticVisitorConfiguration`.
## Order of visit
The types get visited following this order:
- The type itself
- Inherited types
- Encompassing types
- Assignable types (disabled by default)
- Properties