Ejemplo n.º 1
0
        public void Set(string propertyName, object value, bool setNullOrDefaultValue)
        {
            var member = members.FirstOrDefault(a => a.Name == propertyName);

            if (member == null)
            {
                return;
            }

            if (value != null)
            {
                o[propertyName] = value;

                return;
            }

            if (!setNullOrDefaultValue)
            {
                return;
            }
            var isNullable = Nullable.GetUnderlyingType(member.Type) != null || member.Type == s;

            if (isNullable)
            {
                o[propertyName] = null;
            }
            else
            {
                o[propertyName] = Activator.CreateInstance(member.Type);
            }
        }
Ejemplo n.º 2
0
    public static void AssignValue <T>(this TypeAccessor accessor, T t, MemberSet members, string fieldName, object fieldValue)
    {
        var index = fieldName.IndexOf('.');

        if (index == -1)
        {
            if (members.Any(m => string.Equals(m.Name, fieldName, StringComparison.OrdinalIgnoreCase)))
            {
                accessor[t, fieldName] = fieldValue;
            }
        }
        else
        {
            string fieldNameNested = fieldName.Substring(0, index);
            var    member          = members.FirstOrDefault(m => string.Equals(m.Name, fieldNameNested, StringComparison.OrdinalIgnoreCase));

            if (member != null)
            {
                var nestedAccesor = TypeAccessor.Create(member.Type);
                var tNested       = accessor[t, fieldNameNested];

                if (tNested == null)
                {
                    tNested = Activator.CreateInstance(member.Type);
                    accessor[t, fieldNameNested] = tNested;
                }

                nestedAccesor.AssignValue(tNested, nestedAccesor.GetMembers(), fieldName.Substring(index + 1), fieldValue);
            }
        }
    }
Ejemplo n.º 3
0
        public TEntity Assign <TEntity>(TEntity entity, string[] parameterArray, IList value) where TEntity : class
        {
            if (parameterArray?.Count() == 0 || value?.Count == 0)
            {
                return(entity);
            }

            var       access    = TypeAccessor.Create(typeof(TEntity));
            MemberSet members   = access.GetMembers();
            var       newEntity = entity ?? access.CreateNew();

            for (int i = 0; i < parameterArray.Count(); i++)
            {
                if (value[i] != null && !string.IsNullOrEmpty(parameterArray[i]))
                {
                    try
                    {
                        access[newEntity, parameterArray[i]] = value[i];
                    }
                    catch (InvalidCastException ex)
                    {
                        Member member       = members.FirstOrDefault(m => m.Name.ToUpper() == parameterArray[i].ToUpper());
                        Type   propertyType = member.Type;

                        if (propertyType == typeof(long?) || propertyType == typeof(long))
                        {
                            access[newEntity, parameterArray[i]] = Int64.Parse(value[i].ToString());
                        }
                        if (propertyType == typeof(Decimal?) || propertyType == typeof(Decimal))
                        {
                            access[newEntity, parameterArray[i]] = Decimal.Parse(value[i].ToString());
                        }
                        if (propertyType == typeof(Double?) || propertyType == typeof(Double))
                        {
                            access[newEntity, parameterArray[i]] = Double.Parse(value[i].ToString());
                        }
                        if (propertyType == typeof(Int32?) || propertyType == typeof(Int32))
                        {
                            access[newEntity, parameterArray[i]] = Int32.Parse(value[i].ToString());
                        }
                        if (propertyType == typeof(Boolean?) || propertyType == typeof(Boolean))
                        {
                            access[newEntity, parameterArray[i]] = Boolean.Parse(value[i].ToString());
                        }
                        if (propertyType == typeof(DateTime?) || propertyType == typeof(DateTime))
                        {
                            int    separate_index = value[i].ToString().IndexOf(':'), length = value[i].ToString().Length;
                            string date_part = value[i].ToString().Substring(0, separate_index);
                            string time_part = value[i].ToString().Substring(separate_index + 1, length - separate_index - 1);

                            access[newEntity, parameterArray[i]] = Convert.ToDateTime(string.Format("{0} {1}", date_part, time_part));
                        }
                    }
                }
            }

            return((TEntity)newEntity);
        }
Ejemplo n.º 4
0
        private static bool NeedToBeHidden(MemberSet metaProperties, Member propertyToCheck)
        {
            // If meta exists, find matching property
            var metaProperty = (metaProperties == null
                ? null
                : metaProperties.FirstOrDefault(p => p.Name == propertyToCheck.Name));

            // If hide log - don't display
            return(metaProperty != null && metaProperty.IsDefined(typeof(HideLog)));
        }