Microsoft.JScript.LateBinding.SetValueAtIndex C# (CSharp) Method

SetValueAtIndex() private method

private SetValueAtIndex ( Object obj, ulong index, Object value ) : void
obj Object
index ulong
value Object
return void
      internal static void SetValueAtIndex(Object obj, ulong index, Object value){
        if (obj is ScriptObject){
          if (index < UInt32.MaxValue)
            ((ScriptObject)obj).SetValueAtIndex((uint)index, value);
          else
            ((ScriptObject)obj).SetMemberValue(index.ToString(CultureInfo.InvariantCulture), value);
          return;
        }
      default_property_case:
        if (obj is IList){
          IList list = (IList)obj;
          if (index < (ulong)list.Count)
            list[(int)index] = value;
          else
            list.Insert((int)index, value);
          return;
        }
        if (obj is Array){
          checked{((Array)obj).SetValue(Convert.CoerceT(value, obj.GetType().GetElementType()), (int)index);}
          return;
        }
        Type type = obj.GetType();
        if (type.IsCOMObject || obj is IReflect || index > Int32.MaxValue){
          LateBinding.SetMemberValue(obj, index.ToString(CultureInfo.InvariantCulture), value);
          return;
        }

        //Assume that the object does not itself have a field/property with numerical name.
        //See if the object has a default indexed property, or default property that results in an Array or List and work with that.
        MethodInfo setter = JSBinder.GetDefaultPropertyForArrayIndex(type, (int)index, null, true);
        if (setter != null){
          ParameterInfo[] pars = setter.GetParameters();
          if (pars == null || pars.Length == 0){
            obj = setter.Invoke(obj, BindingFlags.SuppressChangeType, null, null, null);
            goto default_property_case;
          }else
            setter.Invoke(obj, (BindingFlags)0, JSBinder.ob, new Object[]{(int)index, value}, null);
        }
      }

Usage Example

        public static Object shift(Object thisob)
        {
            Object res = null;

            if (thisob is ArrayObject)
            {
                return(((ArrayObject)thisob).Shift());
            }
            uint length = Convert.ToUint32(LateBinding.GetMemberValue(thisob, "length"));

            if (length == 0)
            {
                LateBinding.SetMemberValue(thisob, "length", 0);
                return(res);
            }
            res = LateBinding.GetValueAtIndex(thisob, 0);
            for (uint i = 1; i < length; i++)
            {
                Object val = LateBinding.GetValueAtIndex(thisob, i);
                if (val is Missing)
                {
                    LateBinding.DeleteValueAtIndex(thisob, i - 1);
                }
                else
                {
                    LateBinding.SetValueAtIndex(thisob, i - 1, val);
                }
            }
            LateBinding.DeleteValueAtIndex(thisob, length - 1);
            LateBinding.SetMemberValue(thisob, "length", length - 1);
            return(res);
        }
All Usage Examples Of Microsoft.JScript.LateBinding::SetValueAtIndex