// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using Microsoft.AspNet.Mvc.Infrastructure; namespace Microsoft.AspNet.Mvc.ViewComponents { /// /// Default implementation of . /// public class DefaultViewComponentDescriptorProvider : IViewComponentDescriptorProvider { private readonly IAssemblyProvider _assemblyProvider; /// /// Creates a new . /// /// The . public DefaultViewComponentDescriptorProvider(IAssemblyProvider assemblyProvider) { _assemblyProvider = assemblyProvider; } /// public virtual IEnumerable GetViewComponents() { var types = GetCandidateTypes(); return types .Where(IsViewComponentType) .Select(CreateCandidate); } /// /// Gets the candidate instances. The results of this will be provided to /// for filtering. /// /// A list of instances. protected virtual IEnumerable GetCandidateTypes() { var assemblies = _assemblyProvider.CandidateAssemblies; return assemblies.SelectMany(a => a.ExportedTypes).Select(t => t.GetTypeInfo()); } /// /// Determines whether or not the given is a View Component class. /// /// The . /// /// true if represents a View Component class, otherwise false. /// protected virtual bool IsViewComponentType(TypeInfo typeInfo) { if (typeInfo == null) { throw new ArgumentNullException(nameof(typeInfo)); } return ViewComponentConventions.IsComponent(typeInfo); } private static ViewComponentDescriptor CreateCandidate(TypeInfo typeInfo) { var candidate = new ViewComponentDescriptor() { FullName = ViewComponentConventions.GetComponentFullName(typeInfo), ShortName = ViewComponentConventions.GetComponentName(typeInfo), Type = typeInfo.AsType(), }; return candidate; } } }