代码之家  ›  专栏  ›  技术社区  ›  Gavin

索引器的自定义使用[]

  •  10
  • Gavin  · 技术社区  · 16 年前

    mySession["Username"] = "Gav"
    

    5 回复  |  直到 16 年前
        1
  •  22
  •   Rex M    16 年前

    这实际上与编写一个典型的属性几乎相同:

    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"
    
        2
  •  6
  •   Daniel Pryden    16 年前

    MSDN documentation :

    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]);
        }
    }
    
        3
  •  4
  •   Yuliy    16 年前

    C#中的索引器是具有以下名称的属性 this 这里有一个例子。..

    public class Session {
       //...
       public string this[string key]
       {
          get { /* get it from the database */ }
          set { /* store it in the database */ }
       }
    }
    
        4
  •  1
  •   Maharaj    15 年前

    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();
            }
    
        5
  •  0
  •   Jesse C. Slicer    16 年前

    如果你想用你的类来控制ASP。NET会话状态,希望实现 SessionStateStoreProviderBase 类和 IRequiresSessionState

        <sessionState cookieless="true" regenerateExpiredSessionId="true" mode="Custom" customProvider="MySessionProvider">
            <providers>
                <add name="MySessionProvider" type="MySession.MySessionProvider"/>
            </providers>
        </sessionState>
    

    我见过这种用于创建压缩/加密会话状态的技术。