CreateInstanceWithReflectionPermission() приватный метод

private CreateInstanceWithReflectionPermission ( Type type ) : object
type System.Type
Результат object
            /// <devdoc>
            ///     We delegate this to the ClientConfigurationHost. The only thing we need to do here is to
            ///     build a configPath from the ConfigurationUserLevel we get passed in.
            /// </devdoc>
            public override void InitForConfiguration(ref string locationSubPath, out string configPath, out string locationConfigPath,
                                                      IInternalConfigRoot configRoot, params object[] hostInitConfigurationParams)
            {
                ConfigurationUserLevel userLevel = (ConfigurationUserLevel)hostInitConfigurationParams[0];
                string desiredConfigPath         = null;

                Host = (IInternalConfigHost)TypeUtil.CreateInstanceWithReflectionPermission(ClientConfigurationHostTypeName);

                switch (userLevel)
                {
                case ConfigurationUserLevel.None:
                    desiredConfigPath = ClientHost.GetExeConfigPath();
                    break;

                case ConfigurationUserLevel.PerUserRoaming:
                    desiredConfigPath = ClientHost.GetRoamingUserConfigPath();
                    break;

                case ConfigurationUserLevel.PerUserRoamingAndLocal:
                    desiredConfigPath = ClientHost.GetLocalUserConfigPath();
                    break;

                default:
                    throw new ArgumentException(SR.GetString(SR.UnknownUserLevel));
                }


                Host.InitForConfiguration(ref locationSubPath, out configPath, out locationConfigPath, configRoot, null, null, desiredConfigPath);
            }
        internal Configuration(string locationSubPath, Type typeConfigHost, params object[] hostInitConfigurationParams)
        {
            _typeConfigHost = typeConfigHost;
            _hostInitConfigurationParams = hostInitConfigurationParams;

            _configRoot = new InternalConfigRoot(this);

            IInternalConfigHost configHost = (IInternalConfigHost)TypeUtil.CreateInstanceWithReflectionPermission(typeConfigHost);

            // Wrap the host with the UpdateConfigHost to support SaveAs.
            IInternalConfigHost updateConfigHost = new UpdateConfigHost(configHost);

            ((IInternalConfigRoot)_configRoot).Init(updateConfigHost, true);

            //
            // Set the configuration paths for this Configuration.
            // We do this in a separate step so that the WebConfigurationHost
            // can use this object's _configRoot to get the <sites> section,
            // which is used in it's MapPath implementation.
            //
            string configPath, locationConfigPath;

            configHost.InitForConfiguration(ref locationSubPath, out configPath, out locationConfigPath, _configRoot, hostInitConfigurationParams);

            if (!String.IsNullOrEmpty(locationSubPath) && !updateConfigHost.SupportsLocation)
            {
                throw ExceptionUtil.UnexpectedError("Configuration::ctor");
            }

            if (String.IsNullOrEmpty(locationSubPath) != String.IsNullOrEmpty(locationConfigPath))
            {
                throw ExceptionUtil.UnexpectedError("Configuration::ctor");
            }

            // Get the configuration record for this config file.
            _configRecord = (MgmtConfigurationRecord)_configRoot.GetConfigRecord(configPath);

            //
            // Create another MgmtConfigurationRecord for the location that is a child of the above record.
            // Note that this does not match the resolution hiearchy that is used at runtime.
            //
            if (!String.IsNullOrEmpty(locationSubPath))
            {
                _configRecord = MgmtConfigurationRecord.Create(
                    _configRoot, _configRecord, locationConfigPath, locationSubPath);
            }

            //
            // Throw if the config record we created contains global errors.
            //
            _configRecord.ThrowIfInitErrors();
        }
        private ConfigurationBuilder CreateAndInitializeBuilderWithAssert(Type t, ProviderSettings ps)
        {
            ConfigurationBuilder builder     = (ConfigurationBuilder)TypeUtil.CreateInstanceWithReflectionPermission(t);
            NameValueCollection  pars        = ps.Parameters;
            NameValueCollection  cloneParams = new NameValueCollection(pars.Count);

            foreach (string key in pars)
            {
                cloneParams[key] = pars[key];
            }

            builder.Initialize(ps.Name, cloneParams);
            return(builder);
        }
        public ConfigurationProperty(String name, Type type)
        {
            object defaultValue = null;

            ConstructorInit(name, type, ConfigurationPropertyOptions.None, null, null);

            if (type == typeof(string))
            {
                defaultValue = String.Empty;
            }
            else if (type.IsValueType)
            {
                defaultValue = TypeUtil.CreateInstanceWithReflectionPermission(type);
            }
            SetDefaultValue(defaultValue);
        }
        private ConfigurationBuilder CreateAndInitializeBuilderWithAssert(Type t, ProviderSettings ps)
        {
            ConfigurationBuilder builder     = (ConfigurationBuilder)TypeUtil.CreateInstanceWithReflectionPermission(t);
            NameValueCollection  pars        = ps.Parameters;
            NameValueCollection  cloneParams = new NameValueCollection(pars.Count);

            foreach (string key in pars)
            {
                cloneParams[key] = pars[key];
            }

            try {
                builder.Initialize(ps.Name, cloneParams);
            } catch (Exception e) {
                throw ExceptionUtil.WrapAsConfigException(SR.GetString(SR.ConfigBuilder_init_error, ps.Name), e, null);
            }
            return(builder);
        }
Пример #6
0
            private void Init(RuntimeConfigurationRecord configRecord, FactoryRecord factoryRecord)
            {
                // Get the type of the factory
                Type type = TypeUtil.GetTypeWithReflectionPermission(configRecord.Host, factoryRecord.FactoryTypeName, true);

                // If the type is a ConfigurationSection, that's the type.
                if (typeof(ConfigurationSection).IsAssignableFrom(type))
                {
                    _sectionCtor = TypeUtil.GetConstructorWithReflectionPermission(type, typeof(ConfigurationSection), true);
                }
                else
                {
                    // Note: in v1, IConfigurationSectionHandler is in effect a factory that has a Create method
                    // that creates the real section object.

                    // throws if type does not implement IConfigurationSectionHandler
                    TypeUtil.VerifyAssignableType(typeof(IConfigurationSectionHandler), type, true);

                    // Create an instance of the handler
                    _sectionHandler = (IConfigurationSectionHandler)TypeUtil.CreateInstanceWithReflectionPermission(type);
                }
            }
        private void InitDefaultValueFromTypeInfo(ConfigurationPropertyAttribute attribProperty,
                                                  DefaultValueAttribute attribStdDefault)
        {
            object defaultValue = attribProperty.DefaultValue;

            // If there is no default value there - try the other attribute ( the clr standard one )
            if ((defaultValue == null || defaultValue == ConfigurationElement.s_nullPropertyValue) &&
                (attribStdDefault != null))
            {
                defaultValue = attribStdDefault.Value;
            }

            // If there was a default value in the prop attribute - check if we need to convert it from string
            if ((defaultValue != null) && (defaultValue is string) && (_type != typeof(string)))
            {
                // Use the converter to parse this property default value
                try {
                    defaultValue = Converter.ConvertFromInvariantString((string)defaultValue);
                }
                catch (Exception ex) {
                    throw new ConfigurationErrorsException(SR.GetString(SR.Default_value_conversion_error_from_string, _name, ex.Message));
                }
                catch {
                    throw new ConfigurationErrorsException(SR.GetString(SR.Default_value_conversion_error_from_string, _name, ExceptionUtil.NoExceptionInformation));
                }
            }
            if (defaultValue == null || defaultValue == ConfigurationElement.s_nullPropertyValue)
            {
                if (_type == typeof(string))
                {
                    defaultValue = String.Empty;
                }
                else if (_type.IsValueType)
                {
                    defaultValue = TypeUtil.CreateInstanceWithReflectionPermission(_type);
                }
            }
            SetDefaultValue(defaultValue);
        }
        private ProtectedConfigurationProvider CreateAndInitializeProviderWithAssert(Type t, ProviderSettings pn)
        {
            ProtectedConfigurationProvider provider    = (ProtectedConfigurationProvider)TypeUtil.CreateInstanceWithReflectionPermission(t);
            NameValueCollection            pars        = pn.Parameters;
            NameValueCollection            cloneParams = new NameValueCollection(pars.Count);

            foreach (string key in pars)
            {
                cloneParams[key] = pars[key];
            }

            provider.Initialize(pn.Name, cloneParams);
            return(provider);
        }
        internal ConfigurationProperty(PropertyInfo info)
        {
            Debug.Assert(info != null, "info != null");

            // Bellow are the attributes we handle
            TypeConverterAttribute          attribConverter = null;
            ConfigurationPropertyAttribute  attribProperty  = null;
            ConfigurationValidatorAttribute attribValidator = null;

            // Compatability attributes
            // If the approprite data is provided in the ConfigPropAttribute then the one bellow will be ignored
            DescriptionAttribute  attribStdDescription = null;
            DefaultValueAttribute attribStdDefault     = null;

            TypeConverter typeConverter          = null;
            ConfigurationValidatorBase validator = null;

            // Find the interesting attributes in the collection
            foreach (Attribute attribute in Attribute.GetCustomAttributes(info))
            {
                if (attribute is TypeConverterAttribute)
                {
                    attribConverter = (TypeConverterAttribute)attribute;
                    typeConverter   = (TypeConverter)TypeUtil.CreateInstanceWithReflectionPermission(attribConverter.ConverterTypeName);
                }
                else if (attribute is ConfigurationPropertyAttribute)
                {
                    attribProperty = (ConfigurationPropertyAttribute)attribute;
                }
                else if (attribute is ConfigurationValidatorAttribute)
                {
                    if (validator != null)
                    {
                        throw new ConfigurationErrorsException(SR.GetString(SR.Validator_multiple_validator_attributes, info.Name));
                    }

                    attribValidator = (ConfigurationValidatorAttribute)attribute;
                    validator       = attribValidator.ValidatorInstance;
                }
                else if (attribute is DescriptionAttribute)
                {
                    attribStdDescription = (DescriptionAttribute)attribute;
                }
                else if (attribute is DefaultValueAttribute)
                {
                    attribStdDefault = (DefaultValueAttribute)attribute;
                }
            }

            Type propertyType = info.PropertyType;

            // Collections need some customization when the collection attribute is present
            if (typeof(ConfigurationElementCollection).IsAssignableFrom(propertyType))
            {
                ConfigurationCollectionAttribute attribCollection =
                    Attribute.GetCustomAttribute(info,
                                                 typeof(ConfigurationCollectionAttribute)) as ConfigurationCollectionAttribute;

                // If none on the property - see if there is an attribute on the collection type itself
                if (attribCollection == null)
                {
                    attribCollection =
                        Attribute.GetCustomAttribute(propertyType,
                                                     typeof(ConfigurationCollectionAttribute)) as ConfigurationCollectionAttribute;
                }
                if (attribCollection != null)
                {
                    if (attribCollection.AddItemName.IndexOf(',') == -1)
                    {
                        _addElementName = attribCollection.AddItemName;
                    }
                    _removeElementName = attribCollection.RemoveItemName;
                    _clearElementName  = attribCollection.ClearItemsName;
                }
            }

            // This constructor shouldnt be invoked if the reflection info is not for an actual config property
            Debug.Assert(attribProperty != null, "attribProperty != null");

            ConstructorInit(attribProperty.Name,
                            info.PropertyType,
                            attribProperty.Options,
                            validator,
                            typeConverter);

            // Figure out the default value
            InitDefaultValueFromTypeInfo(attribProperty, attribStdDefault);

            // Get the description
            if ((attribStdDescription != null) && !string.IsNullOrEmpty(attribStdDescription.Description))
            {
                _description = attribStdDescription.Description;
            }
        }