To the Apache Age extension has its own data type "agtype". In particular, it looks like this:

Here is the information about this type:
There is a class in C# in which I want Npgsql to map this agtype to me:
/// <summary>Age vertex</summary>
public class Vertex
{
public long Id { get; set; }
public string? Label { get; set; }
public Dictionary<string, object?>? Properties { get; set; }
}
In theory, like any normal Npgsql library, it should somehow be expanded to support additional data types. Npgsql even has some kind of NpgsqlTypeHandler, which seems to be designed for this. How to implement NpgsqlTypeHandler for mapping "ag_catalog.agtype" in C# Class Vertex and how to register the NpgsqlTypeHandler implementation in Npgsql so that it is used for mapping?
Additional details: Npgsql version: 8,0,0 PostgreSQL version: 15.5 Operating system: Windows 10 enterprise .Net 8
Here is the method that I intend to make work:
static async Task SimpleRunQuery()
{
using var dataSource = new NpgsqlDataSourceBuilder(connectionString).Build();
using var connection = await dataSource!.OpenConnectionAsync();
await using var batch = new NpgsqlBatch(connection)
{
BatchCommands = {
new("LOAD 'age';"),
new("SET search_path = ag_catalog, \"$user\", public;")
}
};
await batch.ExecuteNonQueryAsync();
await using var command = new NpgsqlCommand(
@"SELECT * FROM cypher('graph1', $$
MATCH (n:Person)
RETURN n
$$) AS (persons agtype);", connection);
command.AllResultTypesAreUnknown = true;
NpgsqlDataReader reader = await command.ExecuteReaderAsync();
while (await reader.ReadAsync())
{
string result = reader.GetString(0);
Console.WriteLine(result);
//Vertex vertex = reader.GetVertex(0);
//foreach (var property in vertex.Properties)
//{
// Console.WriteLine($"{property.Key}:{property.Value}");
//}
}
}
Without implementing NpgsqlTypeHandler and registering it in Npgsql instead of the following expected result:
{"id": 844424930131969, "label": "Person", "properties": {"age": 23}}::vertex
{"id": 844424930131970, "label": "Person", "properties": {"age": 78}}::vertex
When executing the reader.getString(0) method, the following exception occurs:
Unhandled exception. System.InvalidCastException: Reading as 'System.Object' is not supported for fields having DataTypeName 'ag_catalog.agtype'
at Npgsql.Internal.AdoSerializerHelpers.<GetTypeInfoForReading>g__ThrowReadingNotSupported|0_0(Type type, String displayName, Exception inner)
at Npgsql.Internal.AdoSerializerHelpers.GetTypeInfoForReading(Type type, PostgresType postgresType, PgSerializerOptions options)
at Npgsql.BackendMessages.FieldDescription.<GetInfo>g__GetInfoSlow|50_0(Type type, ColumnInfo& lastColumnInfo)
at Npgsql.BackendMessages.FieldDescription.GetInfo(Type type, ColumnInfo& lastColumnInfo)
at Npgsql.BackendMessages.FieldDescription.get_ObjectOrDefaultInfo()
at Npgsql.BackendMessages.FieldDescription.GetInfo(Type type, ColumnInfo& lastColumnInfo)
at Npgsql.NpgsqlDataReader.<GetInfo>g__Slow|133_0(ColumnInfo& info, PgConverter& converter, Size& bufferRequirement, Boolean& asObject, <>c__DisplayClass133_0&)
at Npgsql.NpgsqlDataReader.GetFieldValueCore[T](Int32 ordinal)
at Npgsql.NpgsqlDataReader.GetString(Int32 ordinal)
at PostgresAge.Program.SimpleRunQuery() in C:\Temp\GitHub\glazkovalex\pg-age\examples\PostgresAge\Program.cs:line 64
at PostgresAge.Program.SimpleRunQuery() in C:\Temp\GitHub\glazkovalex\pg-age\examples\PostgresAge\Program.cs:line 62
at PostgresAge.Program.Main(String[] args) in C:\Temp\GitHub\glazkovalex\pg-age\examples\PostgresAge\Program.cs:line 15
at PostgresAge.Program.<Main>(String[] args)
Please tell me how to implement NpgsqlTypeHandler for mapping "ag_catalog.agtype" in C# Class Vertex and how to register the NpgsqlTypeHandler implementation in Npgsql so that it is used for mapping?