Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 以列表形式访问所有可用的ViewComponents_C#_Asp.net Mvc_Asp.net Core Mvc - Fatal编程技术网

C# 以列表形式访问所有可用的ViewComponents

C# 以列表形式访问所有可用的ViewComponents,c#,asp.net-mvc,asp.net-core-mvc,C#,Asp.net Mvc,Asp.net Core Mvc,我想搜索asp.net核心MVC(mvc6?)项目中所有可用ViewComponents的列表,类似这样的内容 ViewComponent的Default.cshtml foreach (var section in Model.sections) { var sectionId = section.Id; if (_siteSettings.AvailableComponents.Any(c => c == sectionId)) { @awai

我想搜索asp.net核心MVC(mvc6?)项目中所有可用ViewComponents的列表,类似这样的内容

ViewComponent的Default.cshtml

 foreach (var section in Model.sections)
{
    var sectionId = section.Id;
    if (_siteSettings.AvailableComponents.Any(c => c == sectionId))
    {
        @await Component.InvokeAsync(sectionId);
    }
    else
    {
        <p>Could not find component: @sectionId </p>            
    }        
}
foreach(Model.sections中的var节)
{
var sectionId=section.Id;
if(_siteSettings.AvailableComponents.Any(c=>c==sectionId))
{
@wait Component.InvokeAsync(sectionId);
}
其他的
{
找不到组件:@sectionId

} }
现在,我通过在运行时可用的列表中手动注册每个组件来实现这一点。但我想完成的是在每个组件类文件中注册每个视图组件,如下所示:

public class NewsList : ViewComponent
{

    private ISiteSettings _siteSettings;
    private string ComponentName = "NewsList";

    public NewsList(ISiteSettings siteSettings)
    {
        _siteSettings = siteSettings;

        _siteSettings.AvailableComponents.Add(ComponentName);
    }


    public async Task<IViewComponentResult> InvokeAsync()
    {            
        return View();
    }
}
公共类新闻列表:ViewComponent
{
私人ISiteSettings\u站点设置;
私有字符串ComponentName=“NewsList”;
公共新闻列表(ISiteSettings站点设置)
{
_siteSettings=站点设置;
_siteSettings.AvailableComponents.Add(ComponentName);
}
公共异步任务InvokeAsync()
{            
返回视图();
}
}

问题是,在呈现viewcomponent之前,不会执行每个viewcomponent的构造函数,我需要以某种方式“自动”注册所有组件。这可能吗?

要做到这一点,需要使用反射。使用
Assembly
,可以获取项目中的所有类型,然后在
BaseType
typeof(ViewComponent)
的位置进行筛选

var listOfViewComponents = Assembly
                            .GetEntryAssembly()
                            .GetTypes()
                            .Where(x => x.GetTypeInfo().BaseType == typeof(ViewComponent));

希望这能有所帮助。

不知道是否有任何方法可以列出ViewComponent子类的所有类(前提是您通过在项目中继承ViewComponent来创建视图组件)。在中间件级别,你可以得到那些视图组件的列表吗?很漂亮,很有魅力!我考虑过反射,但我认为可能有一些内置的方式用于ViewComponents。