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

  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. This class allows JSObjects to return fields that are not their own, as if they were. Think of it as a field "delegate".
  17. */
  18. namespace Microsoft.JScript
  19. {
  20.    
  21.     using System;
  22.     using System.Globalization;
  23.     using System.Reflection;
  24.    
  25.     internal sealed class JSWrappedField : JSField, IWrappedMember
  26.     {
  27.         internal FieldInfo wrappedField;
  28.         internal object wrappedObject;
  29.        
  30.         internal JSWrappedField(FieldInfo field, object obj)
  31.         {
  32.             if (field is JSFieldInfo)
  33.                 field = ((JSFieldInfo)field).field;
  34.             this.wrappedField = field;
  35.             this.wrappedObject = obj;
  36.             if (obj is JSObject && !Typeob.JSObject.IsAssignableFrom(field.DeclaringType))
  37.                 if (obj is BooleanObject)
  38.                     this.wrappedObject = ((BooleanObject)obj).value;
  39.                 else if (obj is NumberObject)
  40.                     this.wrappedObject = ((NumberObject)obj).value;
  41.                 else if (obj is StringObject)
  42.                     this.wrappedObject = ((StringObject)obj).value;
  43.         }
  44.        
  45.         public override string Name {
  46.             get { return this.wrappedField.Name; }
  47.         }
  48.        
  49.         public override FieldAttributes Attributes {
  50.             get { return this.wrappedField.Attributes; }
  51.         }
  52.        
  53.         public override Type DeclaringType {
  54.             get { return this.wrappedField.DeclaringType; }
  55.         }
  56.        
  57.         public override Type FieldType {
  58.             get { return this.wrappedField.FieldType; }
  59.         }
  60.        
  61.         internal override string GetClassFullName()
  62.         {
  63.             if (this.wrappedField is JSField)
  64.                 return ((JSField)this.wrappedField).GetClassFullName();
  65.             else
  66.                 return this.wrappedField.DeclaringType.FullName;
  67.         }
  68.        
  69.         internal override object GetMetaData()
  70.         {
  71.             if (this.wrappedField is JSField)
  72.                 return ((JSField)this.wrappedField).GetMetaData();
  73.             else
  74.                 return this.wrappedField;
  75.         }
  76.        
  77.         internal override PackageScope GetPackage()
  78.         {
  79.             if (this.wrappedField is JSField) {
  80.                 return ((JSField)this.wrappedField).GetPackage();
  81.             }
  82.             return null;
  83.         }
  84.        
  85.         public override object GetValue(object obj)
  86.         {
  87.             return this.wrappedField.GetValue(this.wrappedObject);
  88.         }
  89.        
  90.         public object GetWrappedObject()
  91.         {
  92.             return this.wrappedObject;
  93.         }
  94.        
  95.         public override void SetValue(object obj, object value, BindingFlags invokeAttr, Binder binder, CultureInfo locale)
  96.         {
  97.             this.wrappedField.SetValue(this.wrappedObject, value, invokeAttr, binder, locale);
  98.         }
  99.     }
  100. }

Developer Fusion