开发者

Is there a functionality in .NET through which a method can determine which class has call it

开发者 https://www.devze.com 2023-02-13 20:03 出处:网络
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

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.

OR

Is there a functionality in .NET through which a method can determine which class has call it


Yuck.

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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号