using System;
using System.Diagnostics;
public class Program {
public static void Main(string[] args) {
Stub();
Console.ReadLine();
}
public static void Stub() {
Check();
}
public static void Check() {
StackTrace trace = new StackTrace(false);
foreach (StackFrame frame in trace.GetFrames()) {
Console.WriteLine(frame.GetMethod().Name);
}
}
}
Well, it has already occurred to me to use the StackTrace class to check the call stack, but the problem is it's possible for methods to get optimized out, preventing them from showing up in the stack trace, and my code would never know.
In fact, the method has been internal up til now, and that has been fine, but now it needs to be made public because it can legitimately be called from outside the Assembly, but I still want it to only be called from within certain classes.You've piqued my curiosity, and I'm interested as to why you want this behaviour. Perhaps there is another approach to achieve what you need - for example, marking the method as internal rather than public.
Already tried it:Calls to methods within the same type might be optimized out, but I very much doubt calls to public methods in other types would be, which would need to happen for the type not to appear in the call stack. Have you tested this scenario by calling methods between types, rather than in the same type?
using System;
using System.Diagnostics;
public class Program {
public static void Main(string[] args) {
OtherClass.Stub();
Console.ReadLine();
}
public static void Check() {
StackTrace trace = new StackTrace(false);
foreach (StackFrame frame in trace.GetFrames()) {
Console.WriteLine(frame.GetMethod().Name);
}
}
}
public class OtherClass {
public static void Stub() {
Program.Check();
}
}
Well, I was hoping not to go into it, because I didn't think it was needed to solve the problem at hand. What I made is a home-grown O/R mapper. The class (there are actually many of them tho) and its members have various attributes that tell my "convenience functions" how to access the database on its behalf. As I said, the problem is it's possible for other code to call the convenience functions directly, passing in the Type of the class, and bypassing the code in the class entirely. And because the class has static methods, I can't just toss the convenience functions into a base class.Would it be possible to see an example of this functionality (or more detail about what these convenience functions do)?
It seems an awful lot of effort involved for something labelled as a convenience
I want to be able to put the "convenience functions" in one assembly, some of the classes that use them in another assembly, and maybe even more of them in a different assembly. The "convenience functions" themselves are really just tools that do what they're told, and I'd like to be able to use them from within multiple projects that themselves may or may not be very much related.If the class has such tight dependencies on the callers then (to me anyway) it suggests that it probably shouldn't be a public class or that the methods in question shouldn't be public.
However without seeing any code it is difficult to say what may be the cleanest solution...