Наследование: ArrayObject
Пример #1
0
      private static Object ToPrimitive(Object value, PreferredType preferredType, IConvertible ic, TypeCode tcode){
        switch (tcode){
          case TypeCode.Object:
            System.Array arr = value as System.Array;
            if (arr != null && arr.Rank == 1) value = new ArrayWrapper(ArrayPrototype.ob, arr, true);
            if (value is ScriptObject){
              Object result = ((ScriptObject)value).GetDefaultValue(preferredType);
              if (Convert.GetTypeCode(result) != TypeCode.Object)
                return result;
              else if (value == result && preferredType == PreferredType.String || preferredType == PreferredType.LocaleString){
                if (value is JSObject){
                  ScriptObject proto = ((JSObject)value).GetParent();
                  if (proto is ClassScope)
                    return ((ClassScope)proto).GetFullName();
                  return "[object Object]";
                }
                return value.ToString();
              }else
                throw new JScriptException(JSError.TypeMismatch);
            }else if (value is Missing || value is System.Reflection.Missing)
              return null;
            else{
              IReflect ir;
              if (value is IReflect && !(value is Type))
                ir = (IReflect)value;
              else
                ir = value.GetType();

              //Look for an op_Explicit conversion to String or Double (this always fails for IDispatch/Ex objects
              MethodInfo meth = null;
              if (preferredType == PreferredType.String || preferredType == PreferredType.LocaleString)
                meth = Convert.GetToXXXXMethod(ir, Typeob.String, true);
              else{
                meth = Convert.GetToXXXXMethod(ir, Typeob.Double, true);
                if (meth == null)
                  meth = Convert.GetToXXXXMethod(ir, Typeob.Int64, true);
                if (meth == null)
                  meth = Convert.GetToXXXXMethod(ir, Typeob.UInt64, true);
              }
              if (meth != null){
                meth = new JSMethodInfo(meth);
                return meth.Invoke(null, BindingFlags.SuppressChangeType, null, new Object[]{value}, null);
              }

              //Invoke the default method/property or get the value of the default field. If an exception is thrown
              //because the target doesn't have a non-paramterized default member, mask it and execute the
              //default handling.
              try{
                try{
                  MemberInfo member = LateBinding.SelectMember(JSBinder.GetDefaultMembers(ir));
                  if (member != null){
                    switch(member.MemberType){
                      case MemberTypes.Field: return ((FieldInfo)member).GetValue(value);
                      case MemberTypes.Method: return ((MethodInfo)member).Invoke(value, new Object[0]);
                      case MemberTypes.Property: return JSProperty.GetValue((PropertyInfo)member, value, null);
                      case MemberTypes.Event: return null;
                      case MemberTypes.NestedType: return member;
                    }
                  }
                  
                  
                  return ir.InvokeMember(String.Empty, BindingFlags.ExactBinding|BindingFlags.SuppressChangeType|
                                                       BindingFlags.InvokeMethod|BindingFlags.GetProperty|BindingFlags.GetField,
                                         null, value, new Object[0], null, null, new String[0]);
                }catch(TargetInvocationException e){
                  throw e.InnerException;
                }
              }catch(ArgumentException){
              }catch(IndexOutOfRangeException){
              }catch(MissingMemberException){
              }catch(TargetParameterCountException){
              }

              if (preferredType != PreferredType.Number)
                if (value is Char[])
                  return new System.String((Char[])value);
                else
                  return value.ToString();
              return value;
            }
          case TypeCode.DateTime:
            return DateConstructor.ob.Construct(ic.ToDateTime(null)).GetDefaultValue(preferredType);
        }
        return value;
      }
        private static object ToPrimitive(object value, PreferredType preferredType, IConvertible ic, TypeCode tcode)
        {
            IReflect reflect;
            TypeCode code = tcode;
            if (code != TypeCode.Object)
            {
                if (code == TypeCode.DateTime)
                {
                    return DateConstructor.ob.Construct(ic.ToDateTime(null)).GetDefaultValue(preferredType);
                }
                return value;
            }
            Array array = value as Array;
            if ((array != null) && (array.Rank == 1))
            {
                value = new ArrayWrapper(ArrayPrototype.ob, array, true);
            }
            if (value is ScriptObject)
            {
                object defaultValue = ((ScriptObject) value).GetDefaultValue(preferredType);
                if (GetTypeCode(defaultValue) != TypeCode.Object)
                {
                    return defaultValue;
                }
                if (((value != defaultValue) || (preferredType != PreferredType.String)) && (preferredType != PreferredType.LocaleString))
                {
                    throw new JScriptException(JSError.TypeMismatch);
                }
                if (!(value is JSObject))
                {
                    return value.ToString();
                }
                ScriptObject parent = ((JSObject) value).GetParent();
                if (parent is ClassScope)
                {
                    return ((ClassScope) parent).GetFullName();
                }
                return "[object Object]";
            }
            if ((value is Microsoft.JScript.Missing) || (value is System.Reflection.Missing))
            {
                return null;
            }
            if ((value is IReflect) && !(value is Type))
            {
                reflect = (IReflect) value;
            }
            else
            {
                reflect = value.GetType();
            }
            MethodInfo method = null;
            if ((preferredType == PreferredType.String) || (preferredType == PreferredType.LocaleString))
            {
                method = GetToXXXXMethod(reflect, typeof(string), true);
            }
            else
            {
                method = GetToXXXXMethod(reflect, typeof(double), true);
                if (method == null)
                {
                    method = GetToXXXXMethod(reflect, typeof(long), true);
                }
                if (method == null)
                {
                    method = GetToXXXXMethod(reflect, typeof(ulong), true);
                }
            }
            if (method != null)
            {
                method = new JSMethodInfo(method);
                return method.Invoke(null, BindingFlags.SuppressChangeType, null, new object[] { value }, null);
            }
            try
            {
                try
                {
                    MemberInfo info2 = LateBinding.SelectMember(JSBinder.GetDefaultMembers(Runtime.TypeRefs, reflect));
                    if (info2 != null)
                    {
                        switch (info2.MemberType)
                        {
                            case MemberTypes.Property:
                                return JSProperty.GetValue((PropertyInfo) info2, value, null);

                            case MemberTypes.NestedType:
                                return info2;

                            case MemberTypes.Event:
                                return null;

                            case MemberTypes.Field:
                                return ((FieldInfo) info2).GetValue(value);

                            case MemberTypes.Method:
                                return ((MethodInfo) info2).Invoke(value, new object[0]);
                        }
                    }
                    if (value == reflect)
                    {
                        Type type = value.GetType();
                        if (TypeReflector.GetTypeReflectorFor(type).Is__ComObject() && (!VsaEngine.executeForJSEE || !(value is IDebuggerObject)))
                        {
                            reflect = type;
                        }
                    }
                    if (VsaEngine.executeForJSEE)
                    {
                        IDebuggerObject obj4 = reflect as IDebuggerObject;
                        if (obj4 != null)
                        {
                            if (!obj4.IsScriptObject())
                            {
                                throw new JScriptException(JSError.NonSupportedInDebugger);
                            }
                            return reflect.InvokeMember("< JScript-" + preferredType.ToString() + " >", BindingFlags.SuppressChangeType | BindingFlags.ExactBinding | BindingFlags.GetProperty | BindingFlags.GetField | BindingFlags.InvokeMethod, null, value, new object[0], null, null, new string[0]);
                        }
                    }
                    return reflect.InvokeMember(string.Empty, BindingFlags.SuppressChangeType | BindingFlags.ExactBinding | BindingFlags.GetProperty | BindingFlags.GetField | BindingFlags.InvokeMethod, null, value, new object[0], null, null, new string[0]);
                }
                catch (TargetInvocationException exception)
                {
                    throw exception.InnerException;
                }
            }
            catch (ArgumentException)
            {
            }
            catch (IndexOutOfRangeException)
            {
            }
            catch (MissingMemberException)
            {
            }
            catch (SecurityException)
            {
            }
            catch (TargetParameterCountException)
            {
            }
            catch (COMException exception2)
            {
                if (exception2.ErrorCode != -2147352573)
                {
                    throw exception2;
                }
            }
            if (preferredType == PreferredType.Number)
            {
                return value;
            }
            if (value.GetType().IsCOMObject)
            {
                return "ActiveXObject";
            }
            if (value is char[])
            {
                return new string((char[]) value);
            }
            return value.ToString();
        }