使用Assembly.GetCallingAssembly()不会返回调用程序集

编程入门 行业动态 更新时间:2024-10-28 00:24:02
本文介绍了使用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()不会返回调用程序集

本文发布于:2023-11-05 16:05:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1561278.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:程序   Assembly   GetCallingAssembly

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!