Exemple #1
0
        public object SetFieldValue(object entity, string fullName, Field fieldInfo, string value)
        {
            string[] parts     = fullName.Split('.');
            Type     enityType = entity.GetType();

            System.Reflection.PropertyInfo pinfo = enityType.GetProperty(parts[0]) ?? throw new Exception(string.Format(ErrorStrings.ERR_PROPERTY_IS_MISSING, enityType.Name,
                                                                                                                        fieldInfo.fieldName));

            if (parts.Length == 1)
            {
                if (!pinfo.CanWrite)
                {
                    throw new Exception(string.Format(ErrorStrings.ERR_PROPERTY_IS_READONLY, enityType.Name,
                                                      fieldInfo.fieldName));
                }

                Type   propType = pinfo.PropertyType;
                object val      = _valueConverter.DeserializeField(propType, fieldInfo, value);

                if (val != null)
                {
                    pinfo.SetValue(entity, val, null);
                }
                else
                {
                    if (propType.IsNullableType())
                    {
                        pinfo.SetValue(entity, val, null);
                    }
                    else if (!propType.IsValueType)
                    {
                        pinfo.SetValue(entity, val, null);
                    }
                    else
                    {
                        throw new Exception(string.Format(ErrorStrings.ERR_FIELD_IS_NOT_NULLABLE, fieldInfo.fieldName));
                    }
                }

                return(val);
            }

            object pval = pinfo.GetValue(entity, null) ?? throw new Exception(string.Format(ErrorStrings.ERR_PPROPERTY_ISNULL, enityType.Name, pinfo.Name));

            return(SetFieldValue(pval, string.Join(".", parts.Skip(1)), fieldInfo, value));
        }