예제 #1
0
        /// <summary>
        /// Calculates a score representing how close the segments match this route. The higher the score the better.
        /// </summary>
        /// <param name="stubs"></param>
        /// <returns></returns>
        public int CalculateRouteScore(string[] segments)
        {
            string[] selfSegments = RouteAttribute.GetSegments();

            //Make sure its all correct
            Debug.Assert(selfSegments.Length == segments.Length);
            if (selfSegments.Length != segments.Length)
            {
                return(0);
            }

            //Caculate the score
            int score = 0;

            for (int i = selfSegments.Length - 1; i >= 0; i--)
            {
                if (selfSegments[i].Equals(segments[i]))
                {
                    score += 10;                                                            //We match exectly, so bonus points
                }
                else if (selfSegments[i][0] == RouteAttribute.ArgumentPrefix)
                {
                    score += 1;                                                             //We match in the arguments, so some points
                }
                else
                {
                    return(0);                                                              //We dont match at all, so abort while we are ahead.
                }
            }

            //Return the calculated score.
            return(score);
        }
예제 #2
0
        public RouteFactory(RouteAttribute routeAttribute, Type routeType)
        {
            RouteAttribute = routeAttribute;
            RouteType      = routeType;


            //Seperate the segments
            string[] segments = routeAttribute.GetSegments();
            Debug.Assert(segments.Length >= 1);

            //Setup teh segment count and base route
            SegmentCount = segments.Length;
            BaseRoute    = segments[0];

            //Create a temporary listing of mappings then find all the variables.
            Dictionary <string, ArgumentMap> mapping = new Dictionary <string, ArgumentMap>();

            for (int i = 0; i < segments.Length; i++)
            {
                Debug.Assert(segments[i].Length >= 2, "Segments need to be at least 2 characters long.");
                if (segments[i].Length >= 2 && segments[i][0] == routeAttribute.ArgumentPrefix)
                {
                    string name = segments[i].Substring(1);
                    mapping.Add(name, new ArgumentMap()
                    {
                        index = i, name = segments[i].Substring(1)
                    });
                }
            }

            //Prepare the eventual setters
            foreach (var property in RouteType.GetProperties())
            {
                var attribute = property.GetCustomAttribute <ArgumentAttribute>();
                if (attribute == null)
                {
                    continue;
                }

                //Get the mapping
                ArgumentMap map;
                if (!mapping.TryGetValue(attribute.ArgumentName, out map))
                {
                    throw new PropertyMappingException(property, attribute.ArgumentName, "Route does not contain any matching arguments.");
                }

                //Some asserts to save our skin while developing
                Debug.Assert(property.CanWrite, "Argument properties must be writable");

                //Make sure it can write
                if (!property.CanWrite)
                {
                    throw new PropertyMappingException(property, attribute.ArgumentName, "Argument properties must be writable");
                }

                //Update the values
                map.property                    = property;
                map.argumentAttribute           = attribute;
                mapping[attribute.ArgumentName] = map;
            }

            //Convert the dictionary into a flat array as that is all we need
            _argmap = mapping.Values.ToArray();
        }