Пример #1
0
        /// <summary>
        /// Instantiate the type handler. If it has a constructor that accepts a TypeHandlerRegistry, use that to allow
        /// the handler to make connector-specific adjustments. Otherwise (the normal case), use the default constructor.
        /// </summary>
        /// <param name="registry"></param>
        /// <returns></returns>
        TypeHandler InstantiateHandler(TypeHandlerRegistry registry)
        {
            Debug.Assert(HandlerType != null);

            if (_ctorWithRegistry == null && _ctorWithoutRegistry == null)
            {
                var ctors = HandlerType.GetConstructors(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
                _ctorWithRegistry = (
                    from c in ctors
                    let p = c.GetParameters()
                            where p.Length == 2 && p[0].ParameterType == typeof(PostgresType) && p[1].ParameterType == typeof(TypeHandlerRegistry)
                            select c
                    ).FirstOrDefault();

                if (_ctorWithRegistry == null)
                {
                    _ctorWithoutRegistry = (
                        from c in ctors
                        let p = c.GetParameters()
                                where p.Length == 1 && p[0].ParameterType == typeof(PostgresType)
                                select c
                        ).FirstOrDefault();
                    if (_ctorWithoutRegistry == null)
                    {
                        throw new Exception($"Type handler type {HandlerType.Name} does not have an appropriate constructor");
                    }
                }
            }

            if (_ctorWithRegistry != null)
            {
                return((TypeHandler)_ctorWithRegistry.Invoke(new object[] { this, registry }));
            }
            Debug.Assert(_ctorWithoutRegistry != null);
            return((TypeHandler)_ctorWithoutRegistry.Invoke(new object[] { this }));
        }