The Labs \ Source Viewer \ SSCLI \ Microsoft.JScript \ JSPrototypeField

  1. // ==++==
  2. //
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. //
  14. // ==--==
  15. /*
  16. An object with a prototype acts as if the properties of its prototype are its own. Any updates to the properties of the prototype are
  17. visible via the object. However, if the property is assigned to via the object, the object makes a local copy of the property and all
  18. further accesses via the object refer to the local copy, not the field of the prototype. Any further updates to the corresponding property
  19. of the prototype will no longer be visible via the object. Likewise, updates via the object do not update the corresponding property on the prototype.
  20. This class provides a wrapper for the fields of prototype properties that implements the desired behavior. When an object is queried for
  21. a property and finds it in its prototype chain, it wraps the prototype property in one these and adds it to its field table. All future accesses
  22. to the property resolve to this field.
  23. Static fields and read only fields are not wrapped since they should not behave like instance fields.
  24. */
  25. namespace Microsoft.JScript
  26. {
  27.    
  28.     using System;
  29.     using System.Globalization;
  30.     using System.Reflection;
  31.    
  32.     internal sealed class JSPrototypeField : JSField
  33.     {
  34.         private object prototypeObject;
  35.         internal FieldInfo prototypeField;
  36.         internal object value;
  37.        
  38.         internal JSPrototypeField(object prototypeObject, FieldInfo prototypeField)
  39.         {
  40.             this.prototypeObject = prototypeObject;
  41.             this.prototypeField = prototypeField;
  42.             this.value = Missing.Value;
  43.         }
  44.        
  45.         public override FieldAttributes Attributes {
  46.             get { return FieldAttributes.Public | FieldAttributes.Static; }
  47.         }
  48.        
  49.         public override object GetValue(object obj)
  50.         {
  51.             if (this.value is Missing)
  52.                 return this.prototypeField.GetValue(this.prototypeObject);
  53.             else
  54.                 //The field has not been written to, defer to the prototype.
  55.                 return this.value;
  56.         }
  57.        
  58.         public override string Name {
  59.             get { return this.prototypeField.Name; }
  60.         }
  61.        
  62.         public override void SetValue(object obj, object value, BindingFlags invokeAttr, Binder binder, CultureInfo locale)
  63.         {
  64.             this.value = value;
  65.         }
  66.     }
  67. }

Developer Fusion