예제 #1
0
파일: ModelConvert.cs 프로젝트: effun/nabla
        private static ModelPropertyInfo[] MapProperty(ModelPropertyInfo targetProperty, ModelInfo sourceModel, ModelConvertOptions options)
        {
            List <ModelPropertyInfo> chain = new List <ModelPropertyInfo>(3);
            string name = options.MapProperty(targetProperty) ?? targetProperty.GetPropertyMap(sourceModel.ModelType);

            if (name != null)
            {
                FillPropertyChain(sourceModel, name, chain);
            }
            else
            {
                var p0 = sourceModel.GetProperty(name = targetProperty.Name);

                if (p0 == null)
                {
                    Stack <ModelPropertyInfo> stack = null;

                    if (FindNestedProperty(name, sourceModel, ref stack))
                    {
                        chain.AddRange(stack);
                    }
                }
                else
                {
                    chain.Add(p0);
                }
            }

            return(chain.ToArray());
        }
예제 #2
0
        /// <summary>
        /// 用于获取模型的属性字典
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        private static List <ModelPropertyInfo> GetPropertyInfos(Type t)
        {
            var modelPropertyInfoList = new List <ModelPropertyInfo>();

            var pis = t.GetProperties();

            foreach (var propertyInfo in pis)
            {
                var modelPropertyInfo = new ModelPropertyInfo {
                    PropertyName = propertyInfo.Name, PropertyType = propertyInfo.PropertyType, PropertyInfo = propertyInfo
                };
                modelPropertyInfo.DefaultValue = modelPropertyInfo.PropertyType.IsValueType ? Activator.CreateInstance(modelPropertyInfo.PropertyType) : null;//获取默认值
                var colummAttr = propertyInfo.GetCustomAttributes(typeof(ColumnAttribute), true);
                if (colummAttr.Length > 0)
                {
                    var colAttr = colummAttr.GetValue(0) as ColumnAttribute;
                    if (colAttr != null)
                    {
                        modelPropertyInfo.ColumnName      = String.IsNullOrEmpty(colAttr.Name) ? modelPropertyInfo.PropertyName : colAttr.Name;
                        modelPropertyInfo.IsPrimaryColumn = colAttr.IsPrimaryKey;
                        modelPropertyInfo.IsAuto          = colAttr.IsAuto;
                        modelPropertyInfo.IsNotNull       = colAttr.IsNotNull;
                    }
                }

                modelPropertyInfoList.Add(modelPropertyInfo);
            }
            return(modelPropertyInfoList);
        }
예제 #3
0
        /// <summary>
        /// Gets the ModelPropertyInfo collection for all the properties of a Model.
        /// </summary>
        /// <param name="targetType">The Model target type.</param>
        /// <returns></returns>
        private static List <ModelPropertyInfo> GetModelProperties(Type targetType)
        {
            var modelPropertyInfos = new List <ModelPropertyInfo>();
            var properties         = targetType.GetProperties(BindingFlags.Instance | BindingFlags.Public);

            foreach (var property in properties)
            {
                var attributes        = property.GetCustomAttributes(false);
                var modelPropertyInfo = new ModelPropertyInfo
                {
                    Name           = property.Name,
                    PropertyInfo   = property,
                    IsRequired     = (property.PropertyType == typeof(Int32)) || ModelReflectionHelper.CheckHasRequiredAttribute(attributes),
                    Length         = ModelReflectionHelper.GetLenghtAttribute(attributes),
                    ExtXType       = ModelReflectionHelper.GetExtXType(property, attributes),
                    Label          = ModelReflectionHelper.GetLabel(attributes) ?? property.Name,
                    DropDownSource = ModelReflectionHelper.GetDropDownAttribute(attributes),
                    Mapping        = ModelReflectionHelper.GetMappingAttribute(attributes)
                };

                modelPropertyInfos.Add(modelPropertyInfo);
            }

            ModelReflector.ProcessDropDowns(modelPropertyInfos);

            return(modelPropertyInfos);
        }
예제 #4
0
파일: WhereBuilder.cs 프로젝트: effun/nabla
        internal static bool ResolvePropertyQueryable(object value, ModelPropertyInfo argumentProperty, WhereBuilderContext context)
        {
            Type argType = context.Arguments.GetType();

            Type[] parameterTypes = new Type[] { typeof(IQueryable <>).MakeGenericType(context.ElementType), argumentProperty.Type };

            var method = argType.GetMethod("Resolve" + argumentProperty.Name,
                                           BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, parameterTypes, null);

            if (method == null && value != null && Nullable.GetUnderlyingType(argumentProperty.Type) == value.GetType())
            {
                parameterTypes[1] = value.GetType();
                method            = argType.GetMethod("Resolve" + argumentProperty.Name,
                                                      BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, parameterTypes, null);
            }

            if (method != null && method.ReturnType == parameterTypes[0])
            {
                var query = method.Invoke(context.Arguments, new object[] { context.CurrentQuery, value });

                if (query != null)
                {
                    context.CurrentQuery = (IQueryable)query;
                }

                return(true);
            }
            else
            {
                return(false);
            }
        }
예제 #5
0
파일: WhereBuilder.cs 프로젝트: effun/nabla
        private static IEnumerable <MemberExpression> MapProperty(ModelPropertyInfo argumentProperty, WhereBuilderContext context)
        {
            var info = context.ElementModel;

            List <string>            names = new List <string>(5);
            List <ModelPropertyInfo> chain = new List <ModelPropertyInfo>(5);

            names.AddRange(argumentProperty.Maps.Where(o => o.SourceType == info.ModelType).Select(o => o.PropertyName));
            if (names.Count == 0)
            {
                names.Add(argumentProperty.Name);
            }

            foreach (string name in names.Distinct())
            {
                chain.Clear();

                ModelConvert.FillPropertyChain(info, name, chain);

                Debug.Assert(chain.Count > 0);

                MemberExpression member = null;

                foreach (var mp in chain)
                {
                    member = Expression.MakeMemberAccess((Expression)member ?? context.Parameter, mp.PropertyInfo);
                }

                yield return(member);
            }
        }
예제 #6
0
//Generates the field configuration for combo boxes
        protected void GenerateComboBoxFieldConfiguration(ModelPropertyInfo property, string fieldName)
        {
            if (property != null)
            {
        #line default
        #line hidden

        #line 88 "D:\projets\Grc.Tool\ED47.Stack\ED47.Stack.Reflector\Templates\JsModel.tt"
                this.Write(",\r\n\t\t\tstore: ED47.Stack.Models.");


        #line default
        #line hidden

        #line 89 "D:\projets\Grc.Tool\ED47.Stack\ED47.Stack.Reflector\Templates\JsModel.tt"
                this.Write(this.ToStringHelper.ToStringWithCulture(fieldName));


        #line default
        #line hidden

        #line 89 "D:\projets\Grc.Tool\ED47.Stack\ED47.Stack.Reflector\Templates\JsModel.tt"
                this.Write("Store,\r\n\t\t\tqueryMode: \"local\",\t\t\t\r\n\t\t\tdisplayField: \"");


        #line default
        #line hidden

        #line 91 "D:\projets\Grc.Tool\ED47.Stack\ED47.Stack.Reflector\Templates\JsModel.tt"
                this.Write(this.ToStringHelper.ToStringWithCulture(property.DisplayField));


        #line default
        #line hidden

        #line 91 "D:\projets\Grc.Tool\ED47.Stack\ED47.Stack.Reflector\Templates\JsModel.tt"
                this.Write("\",\r\n\t\t\tvalueField: \"");


        #line default
        #line hidden

        #line 92 "D:\projets\Grc.Tool\ED47.Stack\ED47.Stack.Reflector\Templates\JsModel.tt"
                this.Write(this.ToStringHelper.ToStringWithCulture(property.ValueField));


        #line default
        #line hidden

        #line 92 "D:\projets\Grc.Tool\ED47.Stack\ED47.Stack.Reflector\Templates\JsModel.tt"
                this.Write("\",\r\n\t\t\tforceSelection: true\r\n\t");


        #line default
        #line hidden

        #line 94 "D:\projets\Grc.Tool\ED47.Stack\ED47.Stack.Reflector\Templates\JsModel.tt"
            }
        }
예제 #7
0
//Generates the store for a combo box
        protected void GenerateComboBoxStore(ModelPropertyInfo property)
        {
            if (property.DropDownSourceProperty != null)
            {
        #line default
        #line hidden

        #line 101 "D:\projets\Grc.Tool\ED47.Stack\ED47.Stack.Reflector\Templates\JsModel.tt"
                this.Write("ED47.Stack.Models.");


        #line default
        #line hidden

        #line 101 "D:\projets\Grc.Tool\ED47.Stack\ED47.Stack.Reflector\Templates\JsModel.tt"
                this.Write(this.ToStringHelper.ToStringWithCulture(property.Name));


        #line default
        #line hidden

        #line 101 "D:\projets\Grc.Tool\ED47.Stack\ED47.Stack.Reflector\Templates\JsModel.tt"
                this.Write("Store = Ext.create(\"Ext.data.Store\"){\t\t\r\n\t\tfields: [\"");


        #line default
        #line hidden

        #line 102 "D:\projets\Grc.Tool\ED47.Stack\ED47.Stack.Reflector\Templates\JsModel.tt"
                this.Write(this.ToStringHelper.ToStringWithCulture(property.DropDownSourceProperty.ValueField));


        #line default
        #line hidden

        #line 102 "D:\projets\Grc.Tool\ED47.Stack\ED47.Stack.Reflector\Templates\JsModel.tt"
                this.Write("\", \"");


        #line default
        #line hidden

        #line 102 "D:\projets\Grc.Tool\ED47.Stack\ED47.Stack.Reflector\Templates\JsModel.tt"
                this.Write(this.ToStringHelper.ToStringWithCulture(property.DropDownSourceProperty.DisplayField));


        #line default
        #line hidden

        #line 102 "D:\projets\Grc.Tool\ED47.Stack\ED47.Stack.Reflector\Templates\JsModel.tt"
                this.Write("\"]\r\n\t};\r\n\t");


        #line default
        #line hidden

        #line 104 "D:\projets\Grc.Tool\ED47.Stack\ED47.Stack.Reflector\Templates\JsModel.tt"
            }
        }
예제 #8
0
        public static Criteria Create(object value, ModelPropertyInfo property)
        {
            if (TryCreate(value, property, out Criteria criteria))
            {
                return(criteria);
            }

            throw new NotSupportedException("Unable to convert " + value.GetType() + " to criteria implicitly.");
        }
예제 #9
0
파일: ModelConvert.cs 프로젝트: effun/nabla
 internal PropertyConvertContext(object source, ModelPropertyInfo[] sourcePropertyChain, object target, ModelPropertyInfo targetProperty, object value, ModelConvertOptions options)
 {
     _sourceProperties = sourcePropertyChain;
     TargetProperty    = targetProperty;
     Value             = value;
     Source            = source;
     Target            = target;
     Options           = options;
 }
예제 #10
0
파일: ModelConvert.cs 프로젝트: effun/nabla
 private static void SetPropertyValue(ModelPropertyInfo mp1, PropertyConvertContext context, object state)
 {
     if (!mp1.IsCollection /*|| mp1.Type.IsAssignableFrom(value.GetType()) && !mp1.IsReadOnly*/)
     {
         SetValueScalar(context);
     }
     else
     {
         SetValueCollection(context);
     }
 }
예제 #11
0
        private static StringCriteria String(string value, ModelPropertyInfo property)
        {
            var attr = property?.GetCustomAttribute <StringCriteriaAttribute>();

            if (attr == null)
            {
                return(String(value));
            }

            return(String(value, attr.Operator, attr.IgnoreEmpty));
        }
예제 #12
0
        //public new IActionResult Json(object data)
        //{
        //    var xm = data.GetType().GetCustomAttributes<XioModelAttribute>();
        //    if (xm.Any())
        //    {
        //        var m = xm.FirstOrDefault();
        //        var o = JObject.FromObject(data);
        //        o.Add("$model", m.UrlPart);
        //        return Content(o.ToString(), "application/json");
        //    }
        //    else
        //    {
        //        return base.Json(data);
        //    }
        //}

        private Dictionary <string, ModelInfo> getModelInfo(Type modelType)
        {
            Stack <Type> types = new Stack <Type>();

            types.Push(modelType);
            Dictionary <string, ModelInfo> result = new Dictionary <string, ModelInfo>();

            while (types.TryPop(out Type mt))
            {
                var modelInfo = mt.GetCustomAttribute <XioModelAttribute>();

                ModelInfo mi = new ModelInfo
                {
                    Title   = modelInfo.Title,
                    UrlPart = modelInfo.UrlPart
                };

                foreach (var prop in mt.GetProperties())
                {
                    var p    = new ModelPropertyInfo();
                    var hide = prop.GetCustomAttribute <HideAttribute>();
                    p.IsEditorLink   = prop.GetCustomAttribute <EditorLinkAttribute>() != null;
                    p.UrlPart        = Char.ToLowerInvariant(prop.Name[0]) + prop.Name.Substring(1);
                    p.IsReadOnly     = prop.GetCustomAttribute <ReadOnlyAttribute>()?.IsReadOnly ?? false;
                    p.Priority       = 1;
                    p.ReferenceModel = prop.GetCustomAttribute <ModelReferenceAttribute>()?.Type;

                    if (p.ReferenceModel != null)
                    {
                        var nextModel = getModelType(p.ReferenceModel);
                        types.Push(nextModel);
                    }

                    p.Title = prop.GetCustomAttribute <DisplayNameAttribute>()?.DisplayName ?? prop.Name;
                    if (hide != null)
                    {
                        p.ShowInEditor = !hide.Flags.HasFlag(HideFlags.Editor);
                        p.ShowInList   = !hide.Flags.HasFlag(HideFlags.List);
                    }
                    else
                    {
                        p.ShowInList   = true;
                        p.ShowInEditor = true;
                    }
                    mi.Properties.Add(p);
                }

                result.Add(modelInfo.UrlPart, mi);
            }

            return(result);
        }
예제 #13
0
        public void Search()
        {
            m_DTAnalyseResults.Rows.Clear();
            HasResult = false;

            if (Task == null)
            {
                FileName = string.Empty;
                RaisePropertyChangedEvent("FileName");
            }
            else
            {
                //m_FileAccess = FileAccessFactory.GetFileAccess(Task.PictureSource);

                ModelPropertyInfo vehicleDetailType = DataModel.Constant.SDT_PropertyInfo_VehicleDetailType[0];

                if (m_NVehicleDetailType > -1)
                {
                    vehicleDetailType = DataModel.Constant.SDT_PropertyInfo_VehicleDetailType[m_NVehicleDetailType];
                }

                int vehicleColor = Array.IndexOf(Constant.COLORNAMES_VEHICLEBODY, m_VehicleColor);
                int plateColor   = Array.IndexOf(Constant.COLORNAMES_VEHICLEPLATE, m_PlateColor);
                vehicleColor = Math.Max(vehicleColor, 0);
                plateColor   = Math.Max(plateColor, 0);

                //int count = Framework.Container.Instance.TaskManager.GetQueryCount(
                //    Task, m_PlateNumber, vehicleDetailType, m_Brand, m_ResultType,
                //    vehicleColor, plateColor, out m_SQLQuery);

                //m_ResultPageInfo = new PageInfo(100, count, 0);
                //m_ResultPageInfo.SelectedPageNumberChanged += new EventHandler(ResultPageInfo_SelectedPageNumberChanged);
                //m_Records = Framework.Container.Instance.TaskManager.SwitchPage(m_SQLQuery, m_ResultPageInfo);

                //FileName = string.Format("{0}: ", Task.Name);
                //RaisePropertyChangedEvent("FileName");

                //if (m_Records != null && m_Records.Count > 0)
                //{
                //    foreach (AnalyseRecord record in m_Records)
                //    {
                //        AddResultRow(m_DTAnalyseResults, record);
                //    }
                //    HasResult = true;
                //}
                //else
                //{
                //    Framework.Container.Instance.InteractionService.ShowMessageBox("没有检索到结果");
                //}
            }
        }
예제 #14
0
        private static void BuildProperty(ModelPropertyInfo mp1, PropertyConvertContext context, object foo)
        {
            if (!mp1.IsReadOnly)
            {
                MemberBindingState state  = (MemberBindingState)foo;
                Expression         member = state.Parameter;

                foreach (var mp0 in context.SourcePropertyChain)
                {
                    member = Expression.MakeMemberAccess(member, mp0.PropertyInfo);
                }

                state.Bindings.Add(Expression.Bind(mp1.PropertyInfo, member));
            }
        }
예제 #15
0
        internal bool ShouldSetValue(ModelPropertyInfo property)
        {
            if (property.IsKey)
            {
                if (_ignoreKeys.HasValue && !_ignoreKeys.Value)
                {
                    return(false);
                }
            }

            var options = GetTargetProperty(property);

            if (options != null)
            {
                return(!options.Ignore);
            }

            return(true);
        }
예제 #16
0
파일: WhereBuilder.cs 프로젝트: effun/nabla
        internal static bool ResolvePropertyExpressionOrCriteria(object value, ModelPropertyInfo argumentProperty, WhereBuilderContext context)
        {
            var method = context.Arguments.GetType().GetMethod("Resolve" + argumentProperty.Name,
                                                               BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new Type[] { argumentProperty.Type }, null);

            if (method != null)
            {
                Type retType = typeof(Expression <>).MakeGenericType(typeof(Func <,>).MakeGenericType(context.ElementType, typeof(bool)));

                if (method.ReturnType == retType)
                {
                    LambdaExpression lambda = (LambdaExpression)method.Invoke(context.Arguments, new object[] { value });

                    if (lambda != null)
                    {
                        //ExpressionReplacementUtil util = new ExpressionReplacementUtil(lambda.Parameters[0], context.Parameter);

                        //context.Append(util.Visit(lambda.Body));

                        context.Append(ReplacementVisitor.Replace(lambda.Body, lambda.Parameters[0], context.Parameter));
                    }

                    return(true);
                }
                else if ((typeof(Criteria)).IsAssignableFrom(method.ReturnType))
                {
                    Criteria criteria = (Criteria)method.Invoke(context.Arguments, new object[] { value });

                    if (criteria != null)
                    {
                        ResolvePropertyDefault(criteria, argumentProperty, context);
                    }

                    return(true);
                }
            }

            return(false);
        }
예제 #17
0
파일: WhereBuilder.cs 프로젝트: effun/nabla
        private static void ResolvePropertyDefault(object value, ModelPropertyInfo mp, WhereBuilderContext context)
        {
            Criteria criteria = value as Criteria;

            if (criteria == null)
            {
                criteria = Criteria.Create(value, mp);
            }

            var members = MapProperty(mp, context);

            if (members != null)
            {
                Expression expr = null;

                foreach (var mb in members)
                {
                    var expr1 = criteria.CreateExpression(mb, context);

                    if (expr1 != null)
                    {
                        if (expr == null)
                        {
                            expr = expr1;
                        }
                        else
                        {
                            expr = Expression.OrElse(expr, expr1);
                        }
                    }
                }

                if (expr != null)
                {
                    context.Append(expr);
                }
            }
        }
예제 #18
0
파일: ModelConvert.cs 프로젝트: effun/nabla
        private static object ConvertPropertyValue(object source, Type sourceType, Type targetType, ModelPropertyInfo property, PropertyConvertContext context)
        {
            if (source == null || targetType.IsAssignableFrom(sourceType))
            {
                return(source);
            }

            TypeConverter converter;

            if (property == null)
            {
                converter = TypeDescriptor.GetConverter(targetType);
            }
            else
            {
                converter = property.GetConverter();
            }

            if (converter != null && converter.CanConvertTo(targetType))
            {
                return(converter.ConvertTo(source, targetType));
            }

            Type type;

            if ((type = Nullable.GetUnderlyingType(targetType)) != null)
            {
                if (type == source.GetType())
                {
                    return(source);
                }
            }

            try
            {
                return(System.Convert.ChangeType(source, targetType));
            }
            catch
            {
            }

            if (sourceType.IsClass && targetType.IsClass)
            {
                return(Convert(source, sourceType, targetType, context.Options));
            }

            throw new InvalidOperationException($"Cannot convert type from {source.GetType()} to {targetType}.");
        }
예제 #19
0
        public static bool TryCreate(object value, ModelPropertyInfo property, out Criteria criteria)
        {
            if (value == null)
            {
                criteria = null;
                return(true);
            }

            Type type = value.GetType();

            if (value is Criteria)
            {
                criteria = (Criteria)value;
            }
            else if (value is string)
            {
                criteria = String((string)value, property);
            }
            else if (value == DBNull.Value)
            {
                criteria = IsNull();
            }
            else if (value is IRange range)
            {
                RangePolicyAttribute attr = property?.GetCustomAttribute <RangePolicyAttribute>();

                criteria = Between(range, attr?.Policy ?? default(RangePolicy));
            }
            else if (type.IsArray)
            {
                criteria = Contain((Array)value);
            }
            else if (type.IsGenericType)
            {
                var info = TypeHelpers.GetCollectionInfo(type);

                if (info.IsCollection)
                {
                    criteria = Contain((ICollection)value);
                }
                else
                {
                    Type type1 = Nullable.GetUnderlyingType(type);

                    if (type1 != null)
                    {
                        type = type1;
                    }

                    criteria = null;
                }
            }
            else
            {
                criteria = null;
            }

            if (criteria == null && value is IComparable comparable)
            {
                criteria = Equal(comparable);
            }

            return(criteria != null);
        }
예제 #20
0
 internal protected virtual bool ResolveProperty(object value, ModelPropertyInfo argumentProperty, WhereBuilderContext context)
 {
     return(WhereBuilder.ResolvePropertyQueryable(value, argumentProperty, context) ||
            WhereBuilder.ResolvePropertyExpressionOrCriteria(value, argumentProperty, context));
 }
예제 #21
0
        internal bool ShouldIgnoreReadOnly(ModelPropertyInfo property)
        {
            var ignore = _ignoreReadOnly ?? GetTarget(property.Model.ModelType)?.ReadOnlyIgnored;

            return(ignore.GetValueOrDefault());
        }
예제 #22
0
 private TargetPropertyOptions GetTargetProperty(ModelPropertyInfo property)
 {
     return(GetTarget(property.Model.ModelType)?[property.Name]);
 }
예제 #23
0
 private SourcePropertyOptions GetSourceProperty(ModelPropertyInfo property)
 {
     return(GetSource(property.Model.ModelType)?[property.Name]);
 }
예제 #24
0
 internal string MapProperty(ModelPropertyInfo property)
 {
     return(GetTargetProperty(property)?.MapTo);
 }
예제 #25
0
 internal Func <object, PropertyConvertContext, bool> GetFilter(ModelPropertyInfo property)
 {
     return(GetSourceProperty(property)?.Filter);
 }