嵌套类访问嵌套类的哪些字段?

时间:2022-01-28 22:56:04

I have a web service in C# and would like to have a nested inner class, that abstracts away the session collection, something like this:

我在C#中有一个Web服务,并希望有一个嵌套的内部类,它抽象出会话集合,如下所示:


public class Service : System.Web.Services.WebService
{
    [WebMethod]
    public string Foo(string ticket)
    {
        SessionPool.getSession(ticket);
    }

    private class SessionPool 
    {
        public static Session getSession(string ticket)
        {
            // this is what i want to do, but I can't access Context
            return (Session)Context.Session[ticket];
        }
    }
}

Is it possible to access the HTTP context of the WebService class via a nested class? If not, is there way I can store the reference to it?

是否可以通过嵌套类访问WebService类的HTTP上下文?如果没有,是否可以存储对它的引用?

3 个解决方案

#1


5  

Nested classes in C# aren't like (non-static) inner classes in Java. There is no implicit reference to an instance of the containing class - so you can't use any instance members of the containing class without an explicit reference.

C#中的嵌套类与Java中的(非静态)内部类不同。没有对包含类的实例的隐式引用 - 因此,如果没有显式引用,则不能使用包含类的任何实例成员。

However, you do have access to all private members of the containing class - with a suitable reference for instance members.

但是,您可以访问包含类的所有私有成员 - 具有适用于实例成员的引用。

#2


0  

System.Web.HttpContext.Current

?

#3


0  

I can think of a couple things.

我可以想到几件事。

First, you might try using getContext() instead of just accessing Context. If that works, you're done.

首先,您可以尝试使用getContext()而不是仅仅访问Context。如果有效,那就完成了。

If not, you could pass the Service in as an initializer to your SessionPool. Add a WebService handle to SessionPool that you initialize via a call to setService() before calling getSession() from Foo().

如果没有,您可以将Service作为初始化程序传递给SessionPool。在从Foo()调用getSession()之前,通过调用setService()初始化SessionPool,添加一个WebService句柄。

Although, at that point, why not just pass in the Context as an argument to getSession()?

虽然,在那一点上,为什么不直接将Context作为参数传递给getSession()?

#1


5  

Nested classes in C# aren't like (non-static) inner classes in Java. There is no implicit reference to an instance of the containing class - so you can't use any instance members of the containing class without an explicit reference.

C#中的嵌套类与Java中的(非静态)内部类不同。没有对包含类的实例的隐式引用 - 因此,如果没有显式引用,则不能使用包含类的任何实例成员。

However, you do have access to all private members of the containing class - with a suitable reference for instance members.

但是,您可以访问包含类的所有私有成员 - 具有适用于实例成员的引用。

#2


0  

System.Web.HttpContext.Current

?

#3


0  

I can think of a couple things.

我可以想到几件事。

First, you might try using getContext() instead of just accessing Context. If that works, you're done.

首先,您可以尝试使用getContext()而不是仅仅访问Context。如果有效,那就完成了。

If not, you could pass the Service in as an initializer to your SessionPool. Add a WebService handle to SessionPool that you initialize via a call to setService() before calling getSession() from Foo().

如果没有,您可以将Service作为初始化程序传递给SessionPool。在从Foo()调用getSession()之前,通过调用setService()初始化SessionPool,添加一个WebService句柄。

Although, at that point, why not just pass in the Context as an argument to getSession()?

虽然,在那一点上,为什么不直接将Context作为参数传递给getSession()?