private static HashSet <Type> GetKnownTypes()
        {
            var q = ExportedTypeHelper.FromCurrentAppDomain(LinkAttributeBase.Exists)
                    .SelectMany(LinkAttributeBase.Get)
                    .Select(l => l.ModelType);

            return(new HashSet <Type>(q));
        }
示例#2
0
        private static RoutingTable BuildRoutingTable(string httpMethod)
        {
            var handlerTypes = ExportedTypeHelper.FromCurrentAppDomain(IsHttpMethodHandler)
                               .Where(i => HttpMethodAttribute.Get(i).HttpMethod.Equals(httpMethod, StringComparison.OrdinalIgnoreCase))
                               .ToArray();

            return(new RoutingTableBuilder(handlerTypes).BuildRoutingTable());
        }
示例#3
0
        internal static RoutingTable BuildRoutingTable(string httpMethod)
        {
            var types        = ExportedTypeHelper.FromCurrentAppDomain(IsHttpMethodHandler).ToList();
            var handlerTypes = types
                               .Where(i => HttpMethodAttribute.Matches(i, httpMethod))
                               .ToArray();

            return(new RoutingTableBuilder(handlerTypes).BuildRoutingTable());
        }
示例#4
0
        private static void PopulateContentTypeHandlerFunctions()
        {
            foreach (var exportedType in ExportedTypeHelper.FromCurrentAppDomain(TypeIsContentTypeHandler))
            {
                AddContentTypeHandler(exportedType);
            }

            AddContentTypeHandler(typeof(FormDeserializer));
        }
 /// <summary>
 /// Runs the startup tasks.
 /// </summary>
 public void RunStartupTasks()
 {
     foreach (var type in ExportedTypeHelper.FromCurrentAppDomain(StartupTaskType.IsAssignableFrom))
     {
         if (!(type.IsInterface || type.IsAbstract))
         {
             CreateAndRunTask(type);
         }
     }
 }
示例#6
0
 protected static IEnumerable <TInfo> FindBehaviorTypes <TAttribute, TInfo>(Func <Type, Type, Priority, TInfo> construct)
     where TAttribute : BehaviorAttribute
 {
     foreach (var behaviorType in ExportedTypeHelper.FromCurrentAppDomain(type => IsBehaviorType <TAttribute>(type)))
     {
         var attribute = (TAttribute)Attribute.GetCustomAttribute(behaviorType, typeof(TAttribute));
         if (attribute != null)
         {
             yield return(construct(behaviorType, attribute.ImplementingType, attribute.Priority));
         }
     }
 }
示例#7
0
        private static bool TryCreateInstance <T>(out T instance)
        {
            var implementations = ExportedTypeHelper.FromCurrentAppDomain(IsImplementationOf <T>).ToList();

            if (implementations.Count == 1)
            {
                if (implementations[0].GetConstructor(new Type[0]) != null)
                {
                    {
                        instance = (T)Activator.CreateInstance(implementations[0]);
                        return(true);
                    }
                }
            }
            instance = default(T);
            return(false);
        }
示例#8
0
        public static IEnumerable <Link> GetRootLinks()
        {
            if (_rootLinks == null)
            {
                lock (RootLinksSync)
                {
                    if (_rootLinks == null)
                    {
                        var handlerTypes =
                            ExportedTypeHelper.FromCurrentAppDomain(t => Attribute.IsDefined(t, typeof(RootAttribute)));
                        _rootLinks = handlerTypes.Select(handlerType =>
                                                         CreateLink(handlerType, (RootAttribute)Attribute.GetCustomAttribute(handlerType, typeof(RootAttribute))))
                                     .ToList();
                    }
                }
            }

            return(_rootLinks.AsEnumerable());
        }
示例#9
0
        private static void BuildRoutesForGenericHandlerType(RoutingTable routingTable, Type exportedType, string uriTemplate,
                                                             List <string> respondsToTypes, List <string> respondsWithTypes)
        {
            var genericArgument            = exportedType.GetGenericArguments().Single();
            var genericParameterAttributes = genericArgument.GenericParameterAttributes &
                                             GenericParameterAttributes.SpecialConstraintMask;
            var    constraints  = genericArgument.GetGenericParameterConstraints();
            string templatePart = "{" + genericArgument.Name + "}";

            if (uriTemplate.Contains(templatePart))
            {
                var genericResolver =
                    Attribute.GetCustomAttribute(exportedType, typeof(GenericResolverAttribute)) as
                    GenericResolverAttribute;
                IEnumerable <Type> candidateTypes;
                Func <Type, IEnumerable <string> > getNames;
                if (genericResolver != null)
                {
                    candidateTypes = genericResolver.GetTypes();
                    getNames       = genericResolver.GetNames;
                }
                else
                {
                    candidateTypes = ExportedTypeHelper.FromCurrentAppDomain(t => true);
                    getNames       = t => new[] { t.Name };
                }
                foreach (var validType in candidateTypes)
                {
                    if (!MatchesConstraints(genericParameterAttributes, constraints, validType))
                    {
                        continue;
                    }
                    foreach (var templateName in getNames(validType))
                    {
                        var withTemplate = uriTemplate.Replace(templatePart, templateName);
                        routingTable.Add(withTemplate,
                                         new HandlerTypeInfo(exportedType.MakeGenericType(validType), respondsToTypes,
                                                             respondsWithTypes));
                    }
                }
            }
        }
示例#10
0
        private static ILinkBuilder CreateBuilder(Type modelType)
        {
            var linkList = new List <Link>();

            foreach (var type in ExportedTypeHelper.FromCurrentAppDomain(LinkAttributeBase.Exists))
            {
                var attributesForModel = LinkAttributeBase.Get(type, modelType);
                if (attributesForModel.Count == 0)
                {
                    continue;
                }
                linkList.AddRange(attributesForModel.Select(a => CreateLink(type, a)));
            }

            if (linkList.Count > 0)
            {
                return(new LinkBuilder(linkList));
            }

            return(LinkBuilder.Empty);
        }
 /// <summary>
 /// When implemented in a derived class, should return a list of all valid types for the generic parameter.
 /// </summary>
 /// <returns>
 /// A list of valid types.
 /// </returns>
 public override IEnumerable <Type> GetTypes()
 {
     return(ExportedTypeHelper.FromCurrentAppDomain(t => _regex.IsMatch(t.FullName ?? string.Empty)));
 }
示例#12
0
 private void PopulateRoutingTableWithHandlers(RoutingTable routingTable)
 {
     PopulateRoutingTableWithHandlers(routingTable, ExportedTypeHelper.FromCurrentAppDomain(TypeIsHandler));
 }