Beispiel #1
0
        /// <summary>
        /// Generic Search for Item.
        /// </summary>
        /// <param name="type"></param>
        /// <param name="Locations"></param>
        /// <param name="SubSearch"></param>
        /// <returns></returns>
        public static List <Item> Find(Type type, List <uint> Locations, bool SubSearch)
        {
            if (type == null)
            {
                return(new List <Item>());
            }
            if ((!typeof(Item).IsAssignableFrom(type)) && (!(type.IsInterface)))
            {
                return(new List <Item>());
            }

            if (Locations == null)
            {
                return(new List <Item>());
            }
            if (Locations.Count < 1)
            {
                return(new List <Item>());
            }

            try
            {
                if (!type.IsInterface)
                {
                    var tester = (Item)Activator.CreateInstance(type, new Serial(0));
                    if (tester == null)
                    {
                        return(new List <Item>());
                    }
                }
            }
            catch
            {
                return(new List <Item>());
            }

            var cattributes = type.GetCustomAttributes(false);

            var _sublist = new List <Item>();


            foreach (var attrib in cattributes)
            {
                if (attrib is QuerySearchAttribute)
                {
                    var x = (QuerySearchAttribute)attrib;

                    var xlist = Scanner.Find <Item>(x.Graphics, x.Colors, Locations, SubSearch);

                    var handle = true;

                    var labels = x.Labels;

                    labels.Remove(0);

                    if (x.Labels.Count > 0)
                    {
                        xlist  = xlist.Where(i => ClilocHelper.ContainsAny(i.Properties, x.Labels)).ToList();
                        handle = xlist.Count > 0;
                    }

                    if (handle)
                    {
                        _sublist.AddRange(xlist.Select(xo => Activator.CreateInstance(type, xo.Serial) as Item));
                    }
                }
                else if (attrib is QueryTypeAttribute)
                {
                    var x = (QueryTypeAttribute)attrib;

                    foreach (var a in x.Types)
                    {
                        var xlist = Find(a, Locations, SubSearch);
                        if (xlist.Count > 0)
                        {
                            _sublist.AddRange(xlist);
                        }
                    }
                }
            }

            var rlist = new List <Item>();

            if (_sublist.Count > 0)
            {
                rlist.AddRange(_sublist);
            }

            return(rlist.Distinct().ToList());
        }