我想创建一个与ASP.Net Session类似的对象。
假设我将此对象称为mySession,我想在你做
时这样做mySession["Username"] = "Gav"
如果它不存在,它会将它添加到数据库表中,如果存在则更新它。我可以编写一个方法来执行此操作但不知道如何在使用索引器语法([])时触发它。我从来没有用索引器构建一个做这样的事情的对象。
在有人说什么之前我知道ASP.Net会话可以保存到数据库,但在这种情况下我需要一个稍微简单的自定义解决方案。
以这种方式使用索引器的任何指针或示例都会很棒。
由于
答案 0 :(得分:20)
它实际上与编写典型属性几乎相同:
public class MySessionThing
{
public object this[string key]
{
//called when we ask for something = mySession["value"]
get
{
return MyGetData(key);
}
//called when we assign mySession["value"] = something
set
{
MySetData(key, value);
}
}
private object MyGetData(string key)
{
//lookup and return object
}
private void MySetData(string key, object value)
{
//store the key/object pair to your repository
}
}
唯一的区别是我们使用关键字“this”而不是给它一个正确的名称:
public object MyProperty
^access ^(return) type ^name
modifier
public object this
^ditto ^ditto ^ "name"
答案 1 :(得分:6)
class SampleCollection<T>
{
// Declare an array to store the data elements.
private T[] arr = new T[100];
// Define the indexer, which will allow client code
// to use [] notation on the class instance itself.
// (See line 2 of code in Main below.)
public T this[int i]
{
get
{
// This indexer is very simple, and just returns or sets
// the corresponding element from the internal array.
return arr[i];
}
set
{
arr[i] = value;
}
}
}
// This class shows how client code uses the indexer.
class Program
{
static void Main(string[] args)
{
// Declare an instance of the SampleCollection type.
SampleCollection<string> stringCollection = new SampleCollection<string>();
// Use [] notation on the type.
stringCollection[0] = "Hello, World";
System.Console.WriteLine(stringCollection[0]);
}
}
答案 2 :(得分:4)
C#中的索引器是名为this
的属性。这是一个例子......
public class Session {
//...
public string this[string key]
{
get { /* get it from the database */ }
set { /* store it in the database */ }
}
}
答案 3 :(得分:1)
以下是我对MSDN示例的轻微变化:
public class KeyValueIndexer<K,V>
{
private Dictionary<K, V> myVal = new Dictionary<K, V>();
public V this[K k]
{
get
{
return myVal[k];
}
set
{
myVal.Add( k, value );
}
}
}
人类:
class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string MiddleName { get; set; }
}
用法:
static void Main( string[] args )
{
KeyValueIndexer<string, object> _keyVal = new KeyValueIndexer<string, object>();
_keyVal[ "Person" ] = new Person() { FirstName="Jon", LastName="Doe", MiddleName="S" };
_keyVal[ "MyID" ] = 123;
Console.WriteLine( "My name is {0} {1}, {2}", ( ( Person ) _keyVal [ "Person" ] ).FirstName, ( ( Person ) _keyVal[ "Person" ] ).MiddleName, ( ( Person ) _keyVal[ "Person" ] ).LastName );
Console.WriteLine( "My ID is {0}", ( ( int ) _keyVal[ "MyID" ] ) );
Console.ReadLine();
}
答案 4 :(得分:0)
如果您想使用您的类来控制ASP.NET会话状态,请查看实现SessionStateStoreProviderBase类和IRequiresSessionState接口。然后,您可以将此会话提供程序添加到web.config的system.web部分:
<sessionState cookieless="true" regenerateExpiredSessionId="true" mode="Custom" customProvider="MySessionProvider">
<providers>
<add name="MySessionProvider" type="MySession.MySessionProvider"/>
</providers>
</sessionState>
我已经看到这种技术用于创建压缩/加密的会话状态。