forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDefaultTypeHandlerFactory.cs
More file actions
35 lines (30 loc) · 1.25 KB
/
DefaultTypeHandlerFactory.cs
File metadata and controls
35 lines (30 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
using System;
using System.Reflection;
using Npgsql.PostgresTypes;
namespace Npgsql.TypeHandling
{
/// <summary>
/// A type handler factory used to instantiate Npgsql's built-in type handlers.
/// </summary>
class DefaultTypeHandlerFactory : INpgsqlTypeHandlerFactory
{
readonly Type _handlerType;
internal DefaultTypeHandlerFactory(Type handlerType)
{
// Recursively look for the TypeHandler<T> superclass to extract its T as the
// DefaultValueType
Type? baseClass = handlerType;
while (!baseClass.GetTypeInfo().IsGenericType || baseClass.GetGenericTypeDefinition() != typeof(NpgsqlTypeHandler<>))
{
baseClass = baseClass.GetTypeInfo().BaseType;
if (baseClass == null)
throw new Exception($"Npgsql type handler {handlerType} doesn't inherit from TypeHandler<>?");
}
DefaultValueType = baseClass.GetGenericArguments()[0];
_handlerType = handlerType;
}
public NpgsqlTypeHandler Create(PostgresType pgType, NpgsqlConnection conn)
=> (NpgsqlTypeHandler)Activator.CreateInstance(_handlerType, pgType)!;
public Type DefaultValueType { get; }
}
}