如何编写一个可以用“arr[key]”索引的类(如数组)?

发布于 2024-11-26 03:49:51 字数 127 浏览 4 评论 0原文

就像我们做的那样 Session.Add("LoginUserId", 123); 然后我们就可以访问Session["LoginUserId"],就像一个Array一样,我们如何实现呢?

Like we do Session.Add("LoginUserId", 123);
and then we can access Session["LoginUserId"], like an Array, how do we implement it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

坚持沉默 2024-12-03 03:49:51

您需要一个索引器

public Thing this[string index]
{
    get
    {
         // get the item for that index.
         return YourGetItemMethod(index)
    }
    set
    {
        // set the item for this index. value will be of type Thing.
        YourAddItemMethod(index, value)
    }
}

这将允许您像使用类对象一样使用类对象数组:

MyClass cl = new MyClass();
cl["hello"] = anotherObject;
// etc.

还有一个 教程

附录

您提到您希望在静态类上使用它。这个有点复杂,因为你不能使用静态索引器。如果您想使用索引器,则需要从静态字段或诸如

You need an indexer:

public Thing this[string index]
{
    get
    {
         // get the item for that index.
         return YourGetItemMethod(index)
    }
    set
    {
        // set the item for this index. value will be of type Thing.
        YourAddItemMethod(index, value)
    }
}

This will let you use your class objects like an array:

MyClass cl = new MyClass();
cl["hello"] = anotherObject;
// etc.

There's also a tutorial available if you need more help.

Addendum:

You mention that you wanted this to be available on a static class. That get's a little more complicated, because you can't use a static indexer. If you want to use an indexer, you'd need to access it off of a static Field or some such sorcery as in this answer.

素食主义者 2024-12-03 03:49:51

听起来您需要的只是一个通用字典

var session = new Dictionary<string, object>();

//set value
session.Add("key", value);

//get value
var value = session["key"] as string;

如果您想使其静态,只需将其设为另一个类中的静态成员即可。

public static class SharedStorage
{
   private static Dictionary<string, object> _data = new Dictionary<string,object>();
   public static Dictionary<string, object> Data { get { return _data; } }
}

然后您可以这样访问它,而无需初始化它:

SharedStorage.Data.Add("someKey", "someValue");
string someValue = (string) SharedStorage.Data["someKey"];

如果您想要更具冒险精神并且正在使用 .NET 4,您还可以使用 Expando 对象,类似于 ASP.NET MVC 3 中控制器可用的 ViewBag 成员:

dynamic expando = new ExpandoObject();
expando.UserId = 5;
var userId = (int) expando.UserId;

Sounds like all you need is a generic dictionary.

var session = new Dictionary<string, object>();

//set value
session.Add("key", value);

//get value
var value = session["key"] as string;

If you want to make this static, just make it a static member in another class.

public static class SharedStorage
{
   private static Dictionary<string, object> _data = new Dictionary<string,object>();
   public static Dictionary<string, object> Data { get { return _data; } }
}

Then you can access it as such, without having to initialize it:

SharedStorage.Data.Add("someKey", "someValue");
string someValue = (string) SharedStorage.Data["someKey"];

If you want to be more adventurous and are using .NET 4 you can also use an Expando Object, like the ViewBag member available to controllers in ASP.NET MVC 3:

dynamic expando = new ExpandoObject();
expando.UserId = 5;
var userId = (int) expando.UserId;
一杆小烟枪 2024-12-03 03:49:51

按照您通常使用会话变量的方式,您真正需要的是 像这样的通用词典集合。你真的不需要写一个类。但是,如果您需要添加额外的功能和/或语义,您当然可以用一个类包装集合,然后只包含和索引器。

对于其他集合,请查看 Collections.Generic 命名空间。

With the way you usually use the Session variable all you really need is a generic Dictionary collection like this one. You don't really need to write a class. But if you need to add extra functionality and/or semantics you could certainly wrap the collection with a class and just include and indexer.

For other collections check out the Collections.Generic namespace.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文