예제 #1
0
        private object ConvertFromString(XElement element, ConfigurationPropertyInfo propertyInfo, string value)
        {
            // if target type is string no need to convert
            if (typeof(string).IsAssignableFrom(propertyInfo.PropertyType))
            {
                return(value);
            }
            // if its convertible from string just convert it and return
            if (propertyInfo.Converter != null && propertyInfo.Converter.CanConvertFrom(typeof(string)))
            {
                return(propertyInfo.Converter.ConvertFromString(value));
            }
            // try id lookup on other handlers
            object result = DoItemLookup(propertyInfo.PropertyType, value);

            if (result != null)
            {
                if (!propertyInfo.PropertyType.IsAssignableFrom(result.GetType()))
                {
                    throw new XmlConfigurationException(element, "Id '{0}' lookup found an incompatible result '{1}' (need '{2}').",
                                                        value, result.GetType().Name, propertyInfo.PropertyType.Name);
                }
                return(result);
            }
            throw new XmlConfigurationException(element, "Unable to convert value of property '{0}' to specified type '{1}'",
                                                propertyInfo.PropertyName, propertyInfo.PropertyType.Name);
        }
예제 #2
0
        public void ConfigurationDataProcessor_correctly_finds_property_info()
        {
            ConfigurationPropertyInfo cpi = _processor.GetPropertyInfo("Test", "Id");

            Assert.IsNotNull(cpi);
            Assert.AreEqual("Id", cpi.PropertyName);
            Assert.AreEqual(typeof(string), cpi.PropertyType);
        }
예제 #3
0
        private void SetPropertyValue(XElement element, ConfigurationTypeInfo typeInfo, IConfigurationData <T> data, string propertyName, string propertyValue)
        {
            ConfigurationPropertyInfo propertyInfo = typeInfo.GetPropertyInfo(propertyName);

            if (propertyInfo == null)
            {
                throw new XmlConfigurationException(element, "Unknown property '{0}'", propertyName);
            }

            object value = ConvertFromString(element, propertyInfo, propertyValue);

            propertyInfo.Property.SetValue(data, value, null);
        }
        /// <summary>
        /// Dynamically adds a property to a <see cref="TypeBuilder"/>.
        /// </summary>
        /// <param name="typeBuilder">The <see cref="TypeBuilder"/> to add the property to.</param>
        /// <param name="name">The name of the property.</param>
        /// <param name="configPropertyInfo">Specifies how to construct the property.</param>
        private static void AddProperty(TypeBuilder typeBuilder, string name, ConfigurationPropertyInfo configPropertyInfo)
        {
            // Create the property attribute.
            var propertyAttributes = configPropertyInfo.DefaultValue != null ? PropertyAttributes.HasDefault : PropertyAttributes.None;
            var propertyBuilder    = typeBuilder.DefineProperty(name, propertyAttributes, configPropertyInfo.Type, parameterTypes: null);

            if (configPropertyInfo.Required)
            {
                propertyBuilder.SetCustomAttribute(
                    new CustomAttributeBuilder(typeof(RequiredAttribute).GetConstructor(Type.EmptyTypes),
                                               new object[] { }));
            }

            if (configPropertyInfo.DefaultValue != null)
            {
                propertyBuilder.SetCustomAttribute(
                    new CustomAttributeBuilder(typeof(DefaultValueAttribute).GetConstructor(new[] { configPropertyInfo.DefaultValue.GetType() }),
                                               new[] { configPropertyInfo.DefaultValue }));
            }

            if (configPropertyInfo.ConfigKey != null)
            {
                propertyBuilder.SetCustomAttribute(
                    new CustomAttributeBuilder(
                        typeof(ConfigurationKeyAttribute).GetConstructor(new[] { typeof(string) }),
                        new object[] { configPropertyInfo.ConfigKey }));
            }

            if (configPropertyInfo.ConfigKeyPrefix != null)
            {
                propertyBuilder.SetCustomAttribute(
                    new CustomAttributeBuilder(
                        typeof(ConfigurationKeyPrefixAttribute).GetConstructor(new[] { typeof(string) }),
                        new object[] { configPropertyInfo.ConfigKeyPrefix }));
            }

            // Create the field that the property will get and set.
            var fieldBuilder = typeBuilder.DefineField($"_{name}", configPropertyInfo.Type, FieldAttributes.Private);

            // Create the get method for the property that will return the field.
            var getMethod = typeBuilder.DefineMethod($"get_{name}",
                                                     MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName, configPropertyInfo.Type, parameterTypes: null);

            var getMethodIl = getMethod.GetILGenerator();

            getMethodIl.Emit(OpCodes.Ldarg_0);
            getMethodIl.Emit(OpCodes.Ldfld, fieldBuilder);
            getMethodIl.Emit(OpCodes.Ret);

            propertyBuilder.SetGetMethod(getMethod);

            // Create the set method for the property that will set the field.
            var setMethod = typeBuilder.DefineMethod($"set_{name}",
                                                     MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName, typeof(void),
                                                     new[] { configPropertyInfo.Type });

            var setMethodIl = setMethod.GetILGenerator();

            setMethodIl.Emit(OpCodes.Ldarg_0);
            setMethodIl.Emit(OpCodes.Ldarg_1);
            setMethodIl.Emit(OpCodes.Stfld, fieldBuilder);
            setMethodIl.Emit(OpCodes.Ret);

            propertyBuilder.SetSetMethod(setMethod);
        }