Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Assembly.GetCallingAssembly() does not return the calling assembly

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?

like image 956
Yair Nevet Avatar asked Feb 11 '12 17:02

Yair Nevet


2 Answers

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;
    }
}
like image 147
Darin Dimitrov Avatar answered Oct 16 '22 19:10

Darin Dimitrov


From the GetCallingAssembly MSDN docs:

Returns the Assembly of the method that invoked the currently executing method.

In your case, GetSubClasses is called by GetControllerNames in the same object so it should be returning the helper assembly.

Edit:

From the Remarks on the MSDN docs:

If the method that calls the GetCallingAssembly method is expanded inline by the just-in-time (JIT) compiler, or if its caller is expanded inline, the assembly that is returned by GetCallingAssembly may differ unexpectedly. For example, consider the following methods and assemblies:

Method M1 in assembly A1 calls GetCallingAssembly.

Method M2 in assembly A2 calls M1.

Method M3 in assembly A3 calls M2.

When M1 is not inlined, GetCallingAssembly returns A2. When M1 is inlined, GetCallingAssembly returns A3. Similarly, when M2 is not inlined, GetCallingAssembly returns A2. When M2 is inlined, GetCallingAssembly returns A3.

So assuming the GetSubClasses isn't inlined, it should be returning the Assembly which GetControllerNames belongs to.

like image 1
M.Babcock Avatar answered Oct 16 '22 19:10

M.Babcock