Get Areas associated with an MVC project

前端 未结 3 1181
旧巷少年郎
旧巷少年郎 2021-01-12 17:13

Is there a way I can get the names of the areas in an MVC project?

Here\'s a few ways I can think of:

a) If I had the source, I could browse through the proj

3条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-12 17:34

    This thread was helpful in finding the answer, but nobody here posted it explicitly. Here is what I came up with, but do note you may need to adjust it depending on whether all of your areas are in the same assembly.

    private IEnumerable GetAllAreasRegistered()
    {
        var assembly = this.GetType().Assembly;
        var areaTypes = assembly.GetTypes().Where(t => t.IsSubclassOf(typeof(AreaRegistration)));
        var areas = new List();
        foreach (var type in areaTypes)
        {
            areas.Add((AreaRegistration)Activator.CreateInstance(type));
        }
        return areas;
    }
    

提交回复
热议问题