Is there a functionality in .NET through which a method can determine which class has call it
I 开发者_StackOverflowhave a class in which there is a method and i have some aspx pages which call this method. Now I want to know in this method which page has call it.
ORIs there a functionality in .NET through which a method can determine which class has call itYuck.
Well anyway,
new StackTrace().GetFrame(1).GetMethod().DeclaringType
That will give you the type.
Any reason this is really required? Seems like a spaghetti solution.
Try this code:
System.Diagnostics.StackFrame f = new System.Diagnostics.StackFrame();
Type t = f.GetMethod().DeclaringType;
string name = t.FullName;
string classname = name.Substring((name.IndexOf('.')+1));
source is this link
Why not use an interface?
This way you can also limit the Pages that can use the method.
interface INamedPage {
string Name { get; set; }
}
Here's your page:
public PageOne: Page, INamedPage {
...
public string Name {
get { return "Page One"; }
}
...
}
The method that is being called:
public void WhoIsIt (INamedPage page) {
return "Oh it was " + page.Name + " again!";
}
Above is a simple example. I wouldn't use strings for all of it. Your solution will have to depend on what you want to do with the Page that called it.
Here you go. This should tell you the full url of the page that requested the current one.
Request.Urlreferrer
精彩评论