Пример #1
0
        /// <summary>
        /// Build creation delegate using public .ctors.
        /// </summary>
        TObjectCreator BuildCreator()
        {
            lock (this)
            {
                if (_lazyCreator == null)
                {
                    if (ReflectionUtils.IsInstantiable(_type) && !IsTrait)
                    {
                        var ctors = _type.DeclaredConstructors.Where(c => c.IsPublic && !c.IsStatic && !c.IsPhpHidden()).ToArray();
                        if (ctors.Length != 0)
                        {
                            _lazyCreator = Dynamic.BinderHelpers.BindToCreator(_type.AsType(), ctors);
                        }
                        else
                        {
                            _flags      |= Flags.InstantiationNotAllowed;
                            _lazyCreator = s_inaccessibleCreator;
                        }
                    }
                    else
                    {
                        _flags      |= Flags.InstantiationNotAllowed;
                        _lazyCreator = (_1, _2) => throw PhpException.ErrorException(
                                                 this.IsInterface ? Resources.ErrResources.interface_instantiated :
                                                 this.IsTrait ? Resources.ErrResources.trait_instantiated :
                                                 /*this.IsAbstract*/ Resources.ErrResources.abstract_class_instantiated, // or static class
                                                 this.Name);
                    }
                }
            }

            return(_lazyCreator);
        }
Пример #2
0
        /// <summary>
        /// Build creation delegate using public and protected .ctors.
        /// </summary>
        TObjectCreator BuildCreatorProtected()
        {
            lock (this)
            {
                if (_lazyCreatorProtected == null)
                {
                    if (ReflectionUtils.IsInstantiable(_type) && !IsTrait)
                    {
                        var  ctorsList    = new ValueList <ConstructorInfo>();
                        bool hasProtected = false;
                        foreach (var c in _type.DeclaredConstructors)
                        {
                            if (!c.IsStatic && !c.IsPrivate && !c.IsPhpFieldsOnlyCtor() && !c.IsPhpHidden())
                            {
                                ctorsList.Add(c);
                                hasProtected |= c.IsFamily;
                            }
                        }
                        _lazyCreatorProtected = hasProtected
                            ? Dynamic.BinderHelpers.BindToCreator(_type.AsType(), ctorsList.ToArray())
                            : this.Creator;
                    }
                    else
                    {
                        _flags |= Flags.InstantiationNotAllowed;
                        _lazyCreatorProtected = this.Creator;
                    }
                }
            }

            return(_lazyCreatorProtected);
        }