简体   繁体   中英

c# - How to get reference to object A inside A's class?

In my SharePoint 2010 c# / asp.net site, I have a class defined like

namespace PDF_Library.VisualWebPart1
{
    public partial class PDF_Library : Usercontrol   
    {
         public static PDF_Library current;
         protected void Page_Load(object sender, EventArgs e)
         {
              current = (PDF_Library)this; 
         }
    }
}


public static class Page_State
{
    public static Page is_display()
    {
        return PDF_Library.current.Page; // didn't work...
    }
}

It doesn't have a constructor. How can I get the reference to the current instance of this class ?

I tried something like this in the top

public static PDF_Library current;

Then in a function it had

current = (PDF_Library)this;

But that didn't work...

It is the fact that you used static in the function that was assinging current that this did not work. static is a method that is not tied to any instance of the class, therefor you can not use this .

Your only options are either make the method non-static or pass in a instance of the class as a parameter to the static function.

From what I can tell you are trying to create a " Singleton Pattern ". See the link to the previous MSDN article for examples on how to create a singleton class.

You need to understand that it does not work this way. Your question is tagged with asp.net - multi-user, multi-threaded environment where multiple instances of PDF_Library user control will be created all the time. It is absolutely uncertain which one of them will be hanging off PDF_Library.current . You need to rethink your design.

More on this: Page instance is disposed of when the request processing is finished. Normally this instance with all its controls and things such as Response , Request , Context etc will be set for garbage collection. Because you keep a reference to a UserControl instance in a static field, all these objects (including Page ) will be kept in memory until this current reference is replaced with something else.

This looks like it will have an instance. If the class is marked as static (which it doesn't appear to be) then you can just reference it by name "PDF_Library". Other wise, use ILSpy or reflector to look at the end result. I bet it has a constructor; just because you don't see one, doesn't mean it isn't there. Override the default ctor and set your instance there.

namespace PDF_Library.VisualWebPart1
{
    public partial class PDF_Library : Usercontrol   
    {
        public static PDF_Library Current;
        public PDF_Library() : base() {
           Current = this;
        }

    }
}

The problem you might be having with your Page_Load code is that it's being called too late in the lifecycle and that's why your reference call isn't working.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM