Пример #1
0
        public Type GenerateType(Type interfaceType)
        {
            try
            {
                var name = $"{interfaceType.GetNormalizeInterfaceName()}Impl";

                var existingType = _moduleBuilder.Assembly.GetType(name.Replace("+", "\\+"));

                if (existingType != null)
                {
                    return(existingType);
                }

                var properties = _typePropertiesExtractor.ExtractTypeProperties(interfaceType);

                var typeBuilder = _moduleBuilder.DefineType(name, TypeAttributes.Class | TypeAttributes.Public);

                typeBuilder.AddInterfaceImplementation(interfaceType);

                _propertyCreator.CreateAnonymousProperties(typeBuilder, properties.ToArray(), out _);

                var result = typeBuilder.CreateTypeInfo();

                return(result.AsType());
            }
            catch (Exception e)
            {
                throw new TypeGenerationException(interfaceType, e);
            }
        }
Пример #2
0
        public void PopulateInstanceWithValues(object instance,
                                               Type settings,
                                               SettingsOptions options,
                                               IEnumerable <ISectionBinder> binders)
        {
            var sectionBinders = binders as ISectionBinder[] ?? binders.ToArray();

            foreach (var property in _typePropertiesExtractor.ExtractTypeProperties(settings))
            {
                var tempValue = property.GetDefaultValue();
                foreach (var binder in sectionBinders)
                {
                    var context = new BindingContext(settings.GetSectionName(options),
                                                     property.GetPropertyName(),
                                                     settings,
                                                     property,
                                                     tempValue);

                    try
                    {
                        binder.BindPropertySettings(context);
                        if (context.HasNewValue)
                        {
                            tempValue = context.NewValue;
                        }
                    }
                    catch (Exception e)
                    {
                        throw new SettingsBindingException(binder, context, e);
                    }
                }

                var propertyValue = ConvertPropertyValue(settings, tempValue, property, options);
                property.SetValue(instance, propertyValue);
            }
        }