none
Can I get the calling assembly of the calling assembly of the calling assembly, etc? RRS feed

  • Question

  • Hello,

    I have an app that is split into various assemblies, to various depths (ie Ass1 calls method in Ass2, which calls method in Ass3, etc). I need to get some resources in one of the topmost assemblies, and so need to get that assembly.

    Assembly.GetCallingAssembly() only returns the parent of the current assembly, and I can't see any way to get the parent of that one (and so on).

    Anyone any suggestions? Thanks


    If you're really bored, you could read about my experiments with .NET and some of Microsoft's newer technologies at http://dotnetwhatnot.pixata.co.uk/
    • Moved by Leo Liu - MSFT Monday, October 3, 2011 6:56 AM Moved for better support. (From:Visual C# Language)
    Sunday, October 2, 2011 8:12 PM

Answers

  • There is no magic API/information that would give you "just" the calling assemblies. The only thing you can get to is the callers from call stack (which have the limitation I mentioned above - inlined methods, tail calls, etc. will be lost from the call stack). For each caller you can then get its Assembly.

    -Karel

    • Marked as answer by Paul Zhou Monday, October 10, 2011 3:13 AM
    Tuesday, October 4, 2011 3:46 PM
    Moderator
  • OK, thanks. As it happens, I discovered this morning that there is an internal helper function in Lightswitch that does exactly what I was trying to do, so the question is now academic. Thanks to everyone for the suggestions


    If you're really bored, you could read about my experiments with .NET and some of Microsoft's newer technologies at http://dotnetwhatnot.pixata.co.uk/
    • Marked as answer by Paul Zhou Monday, October 10, 2011 3:13 AM
    Tuesday, October 4, 2011 5:29 PM

All replies

  • Perhaps the entry assembly is the one you want?  Assembly.GetEntryAssmbly

    Or maybe you can hunt it down by a known type that is also contained within that assembly?  Assembly.GetAssembly(Type)

    Sunday, October 2, 2011 8:25 PM
  • you can have all referenced assemblies!

     

    foreach (AssemblyName assemblyName in Assembly.GetExecutingAssembly().GetReferencedAssemblies())
    {
        Assembly assembly = Assembly.Load(assemblyName);
        //NOW, you have refrenced assembly!!!!!
    }
    

     


    Any fool can know. The point is to understand.(Albert Einstein)
    Monday, October 3, 2011 12:06 AM
  • Thanks Ehsan, but this works the wrong way round for me. This code will allow an assembly to see which other assemblies it references. I don't want that. I'm in the situation where a referenced assembly needs to know what other assembly called it, and which called that and so on, back up the call stack.

    Thanks anyway.


    If you're really bored, you could read about my experiments with .NET and some of Microsoft's newer technologies at http://dotnetwhatnot.pixata.co.uk/
    Monday, October 3, 2011 1:41 PM
  • Hello Wyck,

    Thanks for the reply, but unfortunately, it won't work in my case. Assembly.GetEntryAssembly is fine if you want the topmost assembly in the process stack, but I don't. I want to work my way up the stack looking for an assembly which may not be the topmost one.

    Unfortunately, I can't be sure of the type of the assembly I want, so the other idea won't work.

    The situation is that I have some code in a helper DLL that is being called from a Silverlight application. I have a resource in my Silverlight application's topmost class, but this probably won't be the topmost class in the process stack, as Silverlight itself will be higher up, and will have called my assembly. I don't know exactly what calls what, so need to execute code in my helper class that walks up the process tree, looking for an assembly that is the one I wrote. As this code is to be general, I can't specify a fixed type, I can only go by the assembly name, which will be of a fairly specific form.

    So, I'm no further at the moment. Thanks again for the reply, any more ideas?


    If you're really bored, you could read about my experiments with .NET and some of Microsoft's newer technologies at http://dotnetwhatnot.pixata.co.uk/
    Monday, October 3, 2011 1:49 PM
  • Not an expert of Silverlight, but you can get the call stack from the StackTrace class, skip the current frame, then enumerate frames and get the calling assemblies for each method on the stack. As for the loader of Silverlight, I guess it is written in native code and would not be a managed assembly.

    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    Monday, October 3, 2011 7:37 PM
  • Also note that inlined methods will not show up on the stack, so you may skip some caller.caller.caller Assembly. There is no general reliable way, how to get all callers, incl. those that were inlined.

    One safe option is to add argument to your function that will take context or calling assembly. You can then propagate the context from wherever you want.

    -Karel

    Tuesday, October 4, 2011 4:45 AM
    Moderator
  • Thanks for the suggestion Sheng, I looked at the StackTrace, but didn't notice a way to get the actual assembly from it. I'll have to look again and see if I missed that.
    If you're really bored, you could read about my experiments with .NET and some of Microsoft's newer technologies at http://dotnetwhatnot.pixata.co.uk/
    Tuesday, October 4, 2011 8:19 AM
  • Hi Karel, thanks for the reply. If I understand you correctly, then the caller issue isn't a problem for me, as I'm only interested in getting the actual assembly. I'm not worried about that actual caller route.

    However, unfortunately your suggestion can't be used in this case, as I don't have any control over the way my code is called. I'm actually writing a custom control for Lightswitch, which produces a Silverlight application. The way custom controls work is that you declare their usage in the screen designer, and Lightswitch wires up the plumbing for you. Unless you require the developer using the control to write custom code for it (which is one of the main issues a custom control seeks to avoid), you can't add arguments to functions. In truth, there aren't actually any callable functions in there, it's all done with binding.

    Thanks anyway, I'll have another look at the StrackTrace and see what I can do.


    If you're really bored, you could read about my experiments with .NET and some of Microsoft's newer technologies at http://dotnetwhatnot.pixata.co.uk/
    Tuesday, October 4, 2011 8:23 AM
  • There is no magic API/information that would give you "just" the calling assemblies. The only thing you can get to is the callers from call stack (which have the limitation I mentioned above - inlined methods, tail calls, etc. will be lost from the call stack). For each caller you can then get its Assembly.

    -Karel

    • Marked as answer by Paul Zhou Monday, October 10, 2011 3:13 AM
    Tuesday, October 4, 2011 3:46 PM
    Moderator
  • OK, thanks. As it happens, I discovered this morning that there is an internal helper function in Lightswitch that does exactly what I was trying to do, so the question is now academic. Thanks to everyone for the suggestions


    If you're really bored, you could read about my experiments with .NET and some of Microsoft's newer technologies at http://dotnetwhatnot.pixata.co.uk/
    • Marked as answer by Paul Zhou Monday, October 10, 2011 3:13 AM
    Tuesday, October 4, 2011 5:29 PM