Ejemplo n.º 1
0
        private void InitFields(object obj, IEnumerable <FieldInfo> fields)
        {
            var defaultConstr = obj.GetType().GetConstructors()[0];
            var defaultObj    = defaultConstr.Invoke(new object[defaultConstr.GetParameters().Length]);

            foreach (var field in fields)
            {
                var defaultValue = field.GetValue(defaultObj);
                var value        = field.GetValue(obj);

                if ((defaultValue == null && value != null))
                {
                    continue;
                }
                if (value != null && !defaultValue.Equals(value))
                {
                    continue;
                }

                try
                {
                    if (field.FieldType.IsPrimitive ^ (field.FieldType == typeof(string) & field.GetValue(obj) != null))
                    {
                        if (field.FieldType == typeof(string) && !field.GetValue(obj).Equals(""))
                        {
                            continue;
                        }

                        var defValue = Activator.CreateInstance(field.FieldType);
                        var qe       = field.GetValue(obj);

                        if (defValue.Equals(qe))
                        {
                            var generator = _config.GetGeneratorByName(field.Name, obj.GetType());

                            if (generator != null)
                            {
                                field.SetValue(obj, generator.Generate(new GeneratorContext(this, field.FieldType)));
                            }
                            else
                            {
                                field.SetValue(obj, _generators[field.FieldType.ToString()]
                                               ?.Generate(new GeneratorContext(this, field.FieldType)));
                            }
                        }
                    }
                    else
                    {
                        field.SetValue(obj, Create(field.FieldType));
                    }
                }
                catch (Exception)
                {
                    // ignored
                }
            }
        }
Ejemplo n.º 2
0
        private object InitializeWithConstructor(Type type)
        {
            var constructorInfos = type.GetConstructors();
            var constructorInfo  = GetConstructorWithMaxNumberOfParameters(constructorInfos);

            if (constructorInfo == null)
            {
                return(null);
            }

            var constructorParameters = new List <object>();
            var parametersInfo        = constructorInfo.GetParameters();

            foreach (var parameterInfo in parametersInfo)
            {
                var parameterName = parameterInfo.Name;
                var generator     = _config.GetGeneratorByName(parameterName);
                if (generator != null)
                {
                    constructorParameters.Add(generator.Generate());
                }
                else
                {
                    var parameterType = parameterInfo.ParameterType;
                    var parameter     = Create(parameterType);
                    constructorParameters.Add(parameter);
                }
            }
            var instance = constructorInfo.Invoke(constructorParameters.ToArray());

            return(instance);
        }