.NETC # Real Readonly Property

xiaoxiao2021-03-06  42

/ *

.NET / C # Real Readonly Property

When the class private member is a simple type, only the read-only properties can be implemented with the member of the member.

When the class private member is not a simple type (such as: arraylist, hashable, etc.), if only the member provides the member, only the member is only enough! Because this attribute arraylist, hashtable Can be executed with add (..), clear (), remove (...).

After the "Scrupid Saucer" of [Shaped Colorful Yunzhi Jinli], see REFLEctor: arraylist.readonly (...) Static Method to get ReadonlyHashTable!

* /

// using system; // use system.collections; // use system.runtime.serialization

namespace Microshaoft {public class WithReadOnlyPropertyClass {public WithReadOnlyPropertyClass () {this._Hashtable = new System.Collections.Hashtable (); this._Hashtable.Add ( "1", "aaa"); this._Hashtable.Add ( "2", "BBB"); this._hashtable.add ("3", "ccc");

THISTEM.COLLIST = new system.collections.ArrayList (); this._ArrayList.Add ("1"); this._ArrayList.Add ("2"); this._ArrayList.Add ("3");}

private System.Collections.ArrayList _ArrayList; public System.Collections.ArrayList ReadOnlyArrayList {get {//.Net Framework has been achieved return System.Collections.ArrayList.ReadOnly (this._ArrayList);}}

Private system.collections.hashtable _hashtable; public system.collections.hashtable readonlyhashtable {get {return readonlyhashtable.readOnly (this._hashtable);}}

//.Net Framework bother to implement ReadOnlyHashtable ??? private class ReadOnlyHashtable: System.Collections.Hashtable {// "Refactoring: Improving the Design of Existing Code" // 3.21 Refused Bequest: Replace Inheritance with Delegation // If you do not want to modify the superclass, You can also use Replace Inheritance With delegation to achieve your goal. // is to replace inheritance in the subclass, to save the SuperClass object, // remove the SUBCLASS for the inheritance relationship of SuperClass, delegate or call SuperClass to complete the purpose. .Net // commissioned here is not only that the Delegation // agent alternatives, meaning the representative of the private System.Collections.Hashtable _Hashtable;!! Private ReadOnlyHashtable (System.Collections.Hashtable Hashtable) {this._Hashtable = Hashtable; }

public static System.Collections.Hashtable ReadOnly (System.Collections.Hashtable Hashtable) {if (Hashtable == null) {throw new System.ArgumentNullException ( "Hashtable");} // Polymorphic return new ReadOnlyHashtable (Hashtable);}

Private string _s = "Collection is read-only."

// Rewind all the methods of all "Write" operations, runtime errors, if you call this method, throw an exception public override void address {throw new system.notsuPortedException (this._s);}

Public override void clear () {throw new system.notsuPortedException (this._s);}

public override object Clone () {ReadOnlyHashtable roht = new ReadOnlyHashtable (this._Hashtable); roht._Hashtable = (System.Collections.Hashtable) this._Hashtable.Clone (); return roht;}

// Rewrite the Override method public override bool contacts (Object key) {// Rerupted // Return Base.Contains (key) with an instance method of the agent's HashTable Field object; returnid (kEy);

Public Override Bool Containskey (Object Key) {Return this._hashtable.containskey (key);

public override bool ContainsValue (object value) {return this._Hashtable.ContainsValue (value);} public override void CopyTo (System.Array array, int arrayIndex) {this._Hashtable.CopyTo (array, arrayIndex);}

Public override system.collections.idictionaryenumerator getEnumerator () {// rewriting to call the same name method of the proxy member object Return THISHTABLE.GETENUMERATOR ();

// Protected Override int GetHash (Object Key) // {// // Protected member cannot rewrite // Return Base.gethash (KEY); //} //// Protected Override Bool Keyequals Object item, object key) // {// return base.keyequals (item, key); //}

Public Override Void Remove (Object Key) {throw new system.notSupportedException (this._s);

public override void GetObjectData (System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) {this._Hashtable.GetObjectData (info, context);}

Public Override Void ONDSerialization (Object sender) {this._hashtable.ondeserialization (sero);}

Public override bool isreadonly {get {return this._hashtable.isreadonly;}}

Public override bool isfixedsize {get {return this._hashtable.isfixedsize;}}

Public Override Bool Issynchronized {Get {Return THIS._HASHTABLE.issynchronized;}}

Public override system.collections.icolection keys {get {return this._hashtable.keys;}}

Public override system.collections.icollection value {get {return this._hashtable.values;}}

Public override object syncroot {get {return this._hashtable.syncroot;}}

Public override int count {get {return this._hashtable.count;}}

// Indexer Don't forget to override Override Public Override Object this [object key] {Get {// Using proxy object returnid this._hashtable [key];} set {throw new system.notsuPortedException (THIS._S);}} }}} Class appteest {static void main (string [] args) {microshaoft.withreadonlyPropertyClass x = new microshaoft.withreadOnlyPropertyClass ();

System.console.writeline ("ReadonlyArrayList Property Test:"); // Polymorphism System.Collections.ArrayList Al = X.ReadonlyArrayList; Foreach (Object O IN Al) {System.Console.writeline ("Value: {0}" , o);} system.console.writeline (); system.collections.ienumerator ie = al.GeteNumerator (); while (ie.movenext ()) {system.console.writeline ("value: {0}", IE .Current);

System.console.writeline ("Press /" Y / ", execute the following writes will throw an exception, press Other Jump Writing:"); if (System.Console.Readline (). TOLOWER () = = "y") {system.console.writeLine ("Throw an exception ..."); al.clear ();}

System.Console.WriteLine ( "/ nReadOnlyHashTable Property Test:"); // Polymorphic System.Collections.Hashtable ht = x.ReadOnlyHashTable; foreach (System.Collections.DictionaryEntry e in ht) {System.Console.WriteLine ( "Key : {0}, value: {1} ", E.key, E.Value);} system.console.writeline (); system.collections.idictionaryEnumerator IDE = ht.geetenumerator (); while (IDE.MOVENEXT () ) {System.console.writeline ("key: {0}, value: {1}", idE.key, idE.value);} system.console.writeline ("Press /" Y / ", do the following Operation will throw an exception, continue to continue: "); if (system.console.readline (). TOLOWER () ==" y ") {system.console.writeline (" throw exception .. Ht.clear ();} system.console.readline ();}}

转载请注明原文地址:https://www.9cbs.com/read-57678.html

New Post(0)