예제 #1
0
        private static ConditionMappingItemCollection GetMappingItemsBySubClass(RelativeAttributes attrs, MemberInfo sourceMI)
        {
            ConditionMappingItemCollection items = new ConditionMappingItemCollection();

            System.Type subType = attrs.SubClassType != null ? attrs.SubClassType.Type : GetRealType(sourceMI);

            MemberInfo[] mis = GetTypeMembers(subType);

            foreach (SubConditionMappingAttribute attr in attrs.SubClassFieldMappings)
            {
                MemberInfo mi = GetMemberInfoByName(attr.SubPropertyName, mis);

                if (mi != null)
                {
                    ConditionMappingItemBase item = CreateConditionMappingItemByAttr(attr);

                    item.PropertyName         = sourceMI.Name;
                    item.SubClassPropertyName = attr.SubPropertyName;
                    item.MemberInfo           = mi;

                    if (attrs.SubClassType != null)
                    {
                        item.SubClassTypeDescription = attrs.SubClassType.TypeDescription;
                    }

                    item.FillFromAttr(attr);

                    items.Add(item);
                }
            }

            return(items);
        }
예제 #2
0
        private static object GetValueFromObjectDirectly(ConditionMappingItemBase item, object graph)
        {
            object data = GetMemberValueFromObject(item.MemberInfo, graph);

            if (data != null)
            {
                System.Type dataType = data.GetType();

                if (dataType.IsEnum)
                {
                    if (item.EnumUsage == EnumUsageTypes.UseEnumValue)
                    {
                        data = (int)data;
                    }
                    else
                    {
                        data = data.ToString();
                    }
                }
                else
                {
                    if (dataType == typeof(TimeSpan))
                    {
                        data = ((TimeSpan)data).TotalSeconds;
                    }
                }
            }

            return(data);
        }
예제 #3
0
        private static object GetValueFromObject(ConditionMappingItemBase item, object graph)
        {
            object data = null;

            if (item.SubClassPropertyName.IsNullOrEmpty())
            {
                data = GetValueFromObjectDirectly(item, graph);
            }
            else
            {
                if (graph != null)
                {
                    MemberInfo mi = graph.GetType().GetProperty(item.PropertyName,
                                                                BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

                    if (mi == null)
                    {
                        mi = graph.GetType().GetField(item.PropertyName,
                                                      BindingFlags.Instance | BindingFlags.Public);
                    }

                    if (mi != null)
                    {
                        object subGraph = GetMemberValueFromObject(mi, graph);

                        if (subGraph != null)
                        {
                            data = GetValueFromObjectDirectly(item, subGraph);
                        }
                    }
                }
            }

            return(data);
        }
예제 #4
0
        private static ConditionMappingItemBase CreateConditionMappingItemByAttr(ConditionMappingAttributeBase attr)
        {
            ConditionMappingItemBase result = null;

            if (attr is InConditionMappingAttribute)
            {
                result = new InConditionMappingItem();
            }
            else
            {
                result = new ConditionMappingItem();
            }

            return(result);
        }
예제 #5
0
        /// <summary>
        /// 调整数据,然后添加到builder中
        /// </summary>
        /// <param name="data"></param>
        /// <param name="item"></param>
        /// <param name="ignoreDefaultValue"></param>
        /// <param name="acv"></param>
        /// <param name="builderAction"></param>
        private static void DoAdjustValueAndAppendItem(object data, ConditionMappingItemBase item, bool ignoreDefaultValue, AdjustConditionValueDelegate acv, Action <object> builderAction)
        {
            if (data != null)
            {
                //有默认表达式,就不能自动忽略默认值
                bool needIgnoreDefaultValue = ignoreDefaultValue && item.DefaultExpression.IsNullOrEmpty();

                switch (item.DefaultValueUsage)
                {
                case DefaultValueUsageType.Ignore:
                    needIgnoreDefaultValue = true;
                    break;

                case DefaultValueUsageType.UseDefaultValue:
                    needIgnoreDefaultValue = false;
                    break;
                }

                //忽略默认值
                if (needIgnoreDefaultValue == true && IsTypeDefaultValue(item, data))
                {
                    return;
                }

                //不忽略默认值,或者本身就不是默认值
                object adJustedData = null;

                //当默认表达式不为空,且为默认值时
                if (IsTypeDefaultValue(item, data) && item.DefaultExpression.IsNotEmpty())
                {
                    adJustedData = item.DefaultExpression;
                }
                else
                {
                    adJustedData = item.AdjustValue(data);
                }

                bool ignored = false;
                adJustedData = OnAdjustConditionValue(acv, item.PropertyName, adJustedData, ref ignored);

                if (ignored == false && builderAction != null)
                {
                    builderAction(adJustedData);
                }
            }
        }
예제 #6
0
        private static bool IsTypeDefaultValue(ConditionMappingItemBase item, object data)
        {
            bool result = false;

            if (data != null)
            {
                Type type = GetMemberInfoType(item.MemberInfo);

                if (type == typeof(object))
                {
                    type = data.GetType();
                }

                if (type.IsEnum)
                {
                    result = false;

                    if (type == typeof(BooleanState))
                    {
                        result = ((BooleanState)data) == BooleanState.Unknown;
                    }
                    else
                    {
                        result = data.Equals((int)TypeCreator.GetTypeDefaultValue(type));
                    }
                }
                else
                {
                    if (type == typeof(string))
                    {
                        result = string.IsNullOrEmpty((string)data);
                    }
                    else
                    {
                        result = data.Equals(TypeCreator.GetTypeDefaultValue(type));
                    }
                }
            }
            else
            {
                result = true;
            }

            return(result);
        }
예제 #7
0
        private static ConditionMappingItemCollection GetMappingItems(RelativeAttributes attrs, MemberInfo mi)
        {
            ConditionMappingItemCollection items = new ConditionMappingItemCollection();

            ConditionMappingItemBase item = CreateConditionMappingItemByAttr(attrs.FieldMapping);

            item.PropertyName  = mi.Name;
            item.DataFieldName = mi.Name;

            if (attrs.FieldMapping != null)
            {
                item.FillFromAttr(attrs.FieldMapping);
            }

            item.MemberInfo = mi;

            items.Add(item);

            return(items);
        }