简体   繁体   English

ASP.Net普通类内的静态类

[英]Static Class inside Normal class for ASP.Net

I know that making a class static in a ASP.Net project will cause that class's runtime properties to be available to all sessions on the web server. 我知道在ASP.Net项目中将类静态化将导致该类的运行时属性可用于Web服务器上的所有会话。

My question is this: If I declare nested classes static, but the container not, does the sharing of the classes across sessions still apply? 我的问题是:如果我声明嵌套类是静态的,但容器不是静态的,跨会话共享类是否仍然适用?

Example: 例:

 public class FooContainer 
    {

        public static class Bar
        {
        }

        public static class dog
        {
        }
    }

Yes; 是; a static nested class has the same behaviour as any other static class. 静态嵌套类的行为与任何其他静态类相同。 The only time nested classes behave differently is when the outer class is generic type (the nested type is then implicitly generic via the parent, so FooContainer<X>.Bar would be independent of FooContainer<Y>.Bar ). 嵌套类的唯一不同之处是外部类是泛型类型(然后嵌套类型通过父类隐式泛型,因此FooContainer<X>.Bar将独立于FooContainer<Y>.Bar )。

And just be careful: static for sharing data between sessions is fraught with danger. 并且要小心:在会话之间共享数据的静态过程充满了危险。 Synchronize like a paranoid thing. 像一个偏执的事情一样同步。 Personally I would need a very good reason to do this... and it needs careful implementation. 就我个人而言,我将需要一个很好的理由来执行此操作……并且它需要仔细执行。 I only use that approach for things like configuration caching, and even then I'm insanely careful about it. 我只将这种方法用于诸如配置缓存之类的事情,即使如此,我还是对此非常谨慎。

Ultimately, yes. 最终,是的。

The nested static classes are compiled just the same as root-level static classes (Marc wisely notes subtle differences where generics are involved) - the only difference is the qualifying of the path of the type, but this is only natural in any environment and not a side-effect at all. 嵌套的静态类的编译方式与根级别的静态类相同(Marc明智地指出了涉及泛型的细微差异)–唯一的差异是类型路径的限定,但这仅在任何环境中都是自然的,完全没有副作用。

When I see someone say "static data" and "ASP.NET" in the same sentence all sorts of alerts go off. 当我看到有人在同一句话中说“静态数据”和“ ASP.NET”时,所有警报都会响起。

Yes, as Marc said, static classes will be static even if they are nested, you just need to call them via the containing class 是的,正如Marc所说,静态类即使嵌套也将是静态的,您只需要通过包含类调用它们即可

MyStaticClass.StaticField = 3; ///wont work
MyInstanceClass.MyStaticClass.StaticField = 3; // works

Any instantiable class can have static methods/properties/constructors, making a class explicitly static just has the added benefit that you can't make something non-static my mistake, clarifying the purpose and intent of the class. 任何可实例化的类都可以具有静态方法/属性/构造函数,使一个类显式静态化只是具有一个额外的好处,即您不能使非静态化成为我的错误,从而阐明了类的目的和意图。

But beware, static classes in ASP.net are trully static on an application level, so if you have multiple users, they will all see the same static data, regardless of authorizations, and a change of that data will affect all users who use it. 但是请注意,ASP.net中的静态类在应用程序级别上确实是静态的,因此,如果您有多个用户,则无论授权如何,他们都将看到相同的静态数据,并且更改这些数据将影响使用它的所有用户。 。
So a static variable might be the right place to put something that is read once from the db, and then just displayed, ie the current version of the application, or the application's start time, but it's a very wrong place to put user-specific data like language preferences, usernames, etc, etc... 因此,静态变量可能是放置从db中读取一次然后显示的东西的正确位置,例如,应用程序的当前版本或应用程序的启动时间, 但这是放置用户特定的错误位置数据,如语言偏好设置,用户名等,等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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