Walking the Call Stack
Does anyone know how to walk the call stack on a calling assembly? I blogged earlier about a new limit I was writing for XHEO|Licensing. Basically I want to limit an assembly's use at runtime, but I don't know what that assembly is going to be at compile time. I have written some code that loads up all the running assemblies into an array, and walks the array to see if the assembly is loaded. That is not the optimal way however, because someone could just reference and load the required caller, then execute my assembly separately, because the licensed caller is loaded into the AppDomain.
The code to check all the assemblies in the AppDomain looks like this:
1Assembly[] loadedAssemblies = Thread.GetDomain().GetAssemblies();Now. I'm thinking instead, it might be better to create the array with a recursive stack walk, backpedaling through Assembly.GetCallingAssembly(). My question is, is there an easier way to get the call tree from a given assembly? I don't have a whole lot of experience in this particular area of .NET, so I'm sure one of you has a better idea of what to do. Any ideas?
2Assembly asm = null;
3
4for(int i = 0; i < loadedAssemblies.Length; i++)
5 if(String.Compare(loadedAssemblies[i].GetName().Name, AssemblyName, true) == 0)
6 asm = loadedAssemblies[i];
7
8 // Display the full assembly information to the console.
9 if(asm == null)
10 {
11 return false;
12 }