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

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

问题描述

在我的ASP.NET MVC应用程序,我用一个小帮手通过所有控制器进行迭代。
本帮手位于不同的装配比我的MVC应用程序,我引用它。



的问题是,调用Assembly.GetCallingAssembly()的时候在辅助方法,它没有返回MVC应用程序组件,但它返回辅助装配,而不是,这是不是我期望得到,因为我的生活的控制器在MVC应用程序组件,我需要体现出这一点。



我的代码:

  **视图代码(MVC应用程序组件):** 
< NAV>
< UL ID =菜单>
@foreach(新MvcHelper VAR项目()。GetControllerNames())
{
@ Html.ActionMenuItem(
(串)HttpContext.GetGlobalResourceObject(StringsResourse项) ,索引,
项)
}
< / UL>
< / NAV>

**辅助代码(独立组装):**

公共类MvcHelper
{
公开名单<串GT; GetControllerNames()
{
变种controllerNames =新的List<串GT;();
GetSubClasses<控制器与GT;()的ForEach(
型=> controllerNames.Add(type.Name))。
返回controllerNames;
}

私人静态列表<类型> GetSubClasses< T>()
{
返回Assembly.GetCallingAssembly()GetTypes()式(
型=> type.IsSubclassOf(typeof运算(T)))。了ToList() ;
}
}



我在做什么错在这里?


解决方案

我在做什么错在这里?




没有。你可能缺少剃刀意见汇编由ASP.NET运行时单独的程序集的事实。这些组件是动态的。他们什么都没有做与你的ASP.NET MVC应用程序组件。而且,由于你在你的视图调用帮手 Assembly.GetCallingAssembly()方法将返回是这样的:

  App_Web_fqxdopd5,版本= 0.0.0.0,文化=中立,公钥=空

如果你想获得的所有控制器为什么不通过所有引用的程序集只是循环,寻找从各类衍生控制器?您可以使用 AppDomain.CurrentDomain.GetAssemblies()方法。然后为每个组件只 GetTypes()并在过滤器:

 公开类MvcHelper 
{
私人静态列表<类型> GetSubClasses< T>()
{
返回的AppDomain
.CurrentDomain
.GetAssemblies()
.SelectMany(
A => a.GetTypes() 。凡(类型=> type.IsSubclassOf(typeof运算(T)))
).ToList();
}

公开名单<串GT; GetControllerNames()
{
变种controllerNames =新的List<串GT;();
GetSubClasses<控制器与GT;()的ForEach(
型=> controllerNames.Add(type.Name))。
返回controllerNames;
}
}


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

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 and this is not what I'm expecting to get because all my controllers lives at the MVC app assembly and I need to reflect it.

My code:

**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?

解决方案

What am I doing wrong here?

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

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天全站免登陆