使用 Assembly.GetCallingAssembly() 不会返回调用程序集
在我的 ASP.NET MVC 应用程序中,我使用一个小助手来迭代所有控制器。 该助手位于与我的 MVC 应用程序不同的程序集中,我正在引用它。
问题是,当在助手中调用 Assembly.GetCallingAssembly() 方法时,它不会返回 MVC 应用程序集,而是返回助手程序集。 这不是我期望得到的,因为我的所有控制器都位于 MVC 应用程序集中,我需要反映它。
视图代码(MVC 应用程序程序集):
<nav>
<ul id="menu">
@foreach(var item in new MvcHelper().GetControllerNames())
{
@Html.ActionMenuItem(
(string)HttpContext.GetGlobalResourceObject("StringsResourse", item), "Index",
item)
}
</ul>
</nav>
帮助程序代码(独立程序集):
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();
}
}
我在这里做错了什么?
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.
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.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
没有什么。您可能忽略了一个事实:Razor 视图由 ASP.NET 运行时编译为单独的程序集。这些程序集是动态的。它们与 ASP.NET MVC 应用程序集无关。由于您在视图中调用帮助程序,
Assembly.GetCallingAssembly()
方法将返回如下内容:如果您想获取所有控制器,为什么不直接循环遍历所有引用的程序集并查找派生类型来自控制器?您可以使用
AppDomain.CurrentDomain.GetAssemblies()< /code>
方法。然后,对于每个程序集,只需
GetTypes()
并进行过滤: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: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 justGetTypes()
and filter upon:来自
GetCallingAssembly
MSDN 文档< /a>:在您的情况下,
GetSubClasses
由同一对象中的GetControllerNames
调用,因此它应该返回辅助程序集。编辑:
来自 MSDN 文档的备注:
因此,假设
GetSubClasses
未内联,它应该返回GetControllerNames
所属的程序集。From the
GetCallingAssembly
MSDN docs:In your case,
GetSubClasses
is called byGetControllerNames
in the same object so it should be returning the helper assembly.Edit:
From the Remarks on the MSDN docs:
So assuming the
GetSubClasses
isn't inlined, it should be returning the Assembly whichGetControllerNames
belongs to.我相信 GetCallingAssembly 正在工作 - 调用 GetSubClasses 的方法位于您的 MvcHelper 模块(和程序集)中,而不是 MVC 应用程序本身。如果您直接在
GetControllerNames
中调用Assembly.GetCallingAssembly
,您可能会发现得到不同的结果。另请注意,
GetCallingAssembly
的行为可能会有所不同,具体取决于方法是否内联 - 请参阅http://msdn.microsoft.com/en-us/library/system.reflection. assembly.getcalling assembly.aspxI believe
GetCallingAssembly
is working - the method that callsGetSubClasses
is within your MvcHelper module (and assembly) rather than the MVC app itself. If you invokeAssembly.GetCallingAssembly
directly withinGetControllerNames
you may find you get a different result.Also note that the behaviour of
GetCallingAssembly
can vary depending on whether methods are inlined or not - see http://msdn.microsoft.com/en-us/library/system.reflection.assembly.getcallingassembly.aspx