使用Assembly.GetCallingAssembly()不会返回调用程序集 [英] Using Assembly.GetCallingAssembly() does not return the calling assembly

查看:179
本文介绍了使用Assembly.GetCallingAssembly()不会返回调用程序集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的ASP.NET MVC应用程序中,我正在使用一个小的助手来遍历所有控制器. 该帮助程序与我的MVC应用程序位于不同的程序集中,我正在引用它.

In my ASP.NET MVC app I'm using a small helper to iterate through all the controllers. This helper is located at a different assembly than my MVC app and I'm referencing to it.

问题是,当在助手中调用Assembly.GetCallingAssembly()方法时,它不返回MVC应用程序程序集,而是返回助手程序集. 这不是我期望的结果,因为我所有的控制器都生活在MVC应用程序程序集中,因此我需要进行反映.

The problem is, that when calling the Assembly.GetCallingAssembly() method in the helper, it doesn't returns the MVC app assembly, but it returns the helper assembly instead. This is not what I'm expecting to get, because all my controllers are living in the MVC app assembly and I need to reflect it.

视图代码(MVC应用程序程序集):

The view code(MVC app assembly):

<nav>
   <ul id="menu">
      @foreach(var item in new MvcHelper().GetControllerNames())
      {
         @Html.ActionMenuItem(
              (string)HttpContext.GetGlobalResourceObject("StringsResourse", item), "Index",
              item)
      }
   </ul>
</nav>

助手代码(独立程序集):

The Helper code(independent assembly):

public class MvcHelper
{
    public  List<string> GetControllerNames()
    {
        var controllerNames = new List<string>();
        GetSubClasses<Controller>().ForEach(
            type => controllerNames.Add(type.Name));
        return controllerNames;
    }

    private static List<Type> GetSubClasses<T>()
    {
        return Assembly.GetCallingAssembly().GetTypes().Where(
            type => type.IsSubclassOf(typeof(T))).ToList();
    }
}

我在做什么错了?

推荐答案

我在做什么错了?

What am I doing wrong here?

什么都没有.您可能会错过一个事实,即ASP.NET运行时将Razor视图编译为单独的程序集.这些程序集是动态的.它们与您的ASP.NET MVC应用程序程序集无关.并且由于您是在视图中调用帮助程序,因此Assembly.GetCallingAssembly()方法将返回以下内容:

Nothing. You are probably missing the fact that Razor views are compiled as separate assemblies by the ASP.NET runtime. Those assemblies are dynamic. They have nothing to do with your ASP.NET MVC application assembly. And since you are calling the helper in your view the Assembly.GetCallingAssembly() method will return something like this:

App_Web_fqxdopd5, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null

如果要获取所有控制器,为什么不循环遍历所有引用的程序集并查找从Controller派生的类型?您可以为此使用 AppDomain.CurrentDomain.GetAssemblies() 方法.然后,对于每个程序集,只需GetTypes()并过滤:

If you want to get all controllers why not just loop through all referenced assemblies and look for types deriving from Controller? You could use the AppDomain.CurrentDomain.GetAssemblies() method for this. Then for each assembly just GetTypes() and filter upon:

public class MvcHelper
{
    private static List<Type> GetSubClasses<T>()
    {
        return AppDomain
            .CurrentDomain
            .GetAssemblies()
            .SelectMany(
                a => a.GetTypes().Where(type => type.IsSubclassOf(typeof(T)))
            ).ToList();
    }

    public List<string> GetControllerNames()
    {
        var controllerNames = new List<string>();
        GetSubClasses<Controller>().ForEach(
            type => controllerNames.Add(type.Name));
        return controllerNames;
    }
}

这篇关于使用Assembly.GetCallingAssembly()不会返回调用程序集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆