Exemplo n.º 1
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private org.boris.expr.Expr evaluateArrayJS(ExprAbstractEvaluationContext paramExprAbstractEvaluationContext) throws org.boris.expr.ExprException
        private Expr evaluateArrayJS(ExprAbstractEvaluationContext paramExprAbstractEvaluationContext)
        {
            try
            {
                Invocable invocable = (Invocable)Engine;
                object    @object   = invocable.invokeFunction(this.fTable.Name, new object[0]);
                Console.WriteLine("RESULT IS: " + @object);
                if (@object == null)
                {
                    throw new ScriptException("Function " + this.fTable.Name + " does not return an array value", this.fTable.Name, 2);
                }
                if (@object is NativeArray)
                {
                    return(ExprArrayUtil.toExprArray((NativeArray)@object));
                }
                if (@object is object[])
                {
                    return(ExprArrayUtil.toExprArray((object[])@object));
                }
                if (@object is object[][])
                {
                    return(ExprArrayUtil.toExprArray((object[][])@object));
                }
//JAVA TO C# CONVERTER WARNING: The .NET Type.FullName property will not always yield results identical to the Java Class.getName method:
                Console.WriteLine("ARRAY IS OF TYPE: " + @object + " OR " + @object.GetType().FullName);
                throw new ScriptException("Function " + this.fTable.Name + " does not return an array value", this.fTable.Name, 2);
            }
            catch (ScriptException scriptException)
            {
                string str = scriptException.Message;
                if (scriptException.InnerException != null)
                {
                    str = scriptException.InnerException.Message;
                }
                if (str.IndexOf(":", StringComparison.Ordinal) != -1)
                {
                    str = str.Substring(str.LastIndexOf(":", StringComparison.Ordinal) + 1);
                }
                throw new ExprException(str);
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.ToString());
                Console.Write(exception.StackTrace);
                throw new ExprException(exception.Message);
            }
        }
Exemplo n.º 2
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private org.boris.expr.Expr evaluateIntegerJS(ExprAbstractEvaluationContext paramExprAbstractEvaluationContext) throws org.boris.expr.ExprException
        private Expr evaluateIntegerJS(ExprAbstractEvaluationContext paramExprAbstractEvaluationContext)
        {
            try
            {
                Invocable invocable = (Invocable)Engine;
                object    @object   = invocable.invokeFunction(this.fTable.Name, new object[0]);
                if (@object == null)
                {
                    throw new ScriptException("Function " + this.fTable.Name + " does not return an integer value", this.fTable.Name, 2);
                }
                return(new ExprInteger(int.Parse(@object.ToString())));
            }
            catch (ScriptException scriptException)
            {
                string str = scriptException.Message;
                if (scriptException.InnerException != null)
                {
                    str = scriptException.InnerException.Message;
                }
                if (str.IndexOf(":", StringComparison.Ordinal) != -1)
                {
                    str = str.Substring(str.LastIndexOf(":", StringComparison.Ordinal) + 1);
                }
                throw new ExprException(str);
            }
            catch (System.FormatException)
            {
                throw new ExprException("Function " + this.fTable.Name + " does not return an integer value");
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.ToString());
                Console.Write(exception.StackTrace);
                throw new ExprException(exception.Message);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Converts an object of a given formal type to string
        /// representation which can be unmarshalled (converted back to
        /// object) on a target computer.
        /// </summary>
        /// <remarks>
        /// It supports objects of following (informal) categories/types:
        /// primitive, string, IRemotelyCloneable, IRemotelyReferable,
        /// delegate, System.Void, [Serializable] and array. A null object also
        /// can be marshalled.
        /// </remarks>
        /// <param name="obj">An object to be marshalled. Can be null.
        /// </param>
        /// <param name="type">Formal type of the object (only this type will
        /// be available on a target computer). Must not be null.</param>
        /// <returns>Marshalled string representation of given object.
        /// </returns>
        /// <exception cref="ArgumentNullException" />
        /// <exception cref="ArgumentException">If the object type is not
        /// supported for marshalling.</exception>
        public static string Marshal(object obj, Type type)
        {
            // Parameter 'type' must not be null by contract.
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            if (type == typeof(object))
            {
                type = DetermineFormalTypeFromObject(obj);
            }

            Func<Type, string, string> composeResultFromFormalTypeAndData = delegate(Type formalType, string data)
            {
                return formalType.Assembly.FullName + "!" + formalType.FullName + ":" + data;
            };

            if (type.IsPrimitive)
            {
                string result;
                if (obj is IFormattable)
                {
                    result = ((IFormattable)obj).ToString(null, CultureInfo.InvariantCulture);
                }
                else
                {
                    result = obj.ToString();
                }
                return composeResultFromFormalTypeAndData(type, result);
            }

            if ((type.FullName == "System.Void") || (obj == null))
            {
                return composeResultFromFormalTypeAndData(Type.GetType("System.Void"), string.Empty);
            }

            if (type == typeof(string))
            {
                return composeResultFromFormalTypeAndData(typeof(string), (string)obj);
            }

            if (typeof(IRemotelyCloneable).IsAssignableFrom(type))
            {
                var clonable = (IRemotelyCloneable)obj;
                string result = clonable.SerializeClone();
                return composeResultFromFormalTypeAndData(type, result);
            }

            if (typeof(IRemotelyReferable).IsAssignableFrom(type))
            {
                if (!type.IsInterface)
                {
                    throw new ArgumentException("The formal type of the remotely referable object must be an interface.");
                }

                var remotelyReferable = (IRemotelyReferable)obj;
                var address = ObjectServer.PublishObject(remotelyReferable);
                string result = address.Serialize();
                return composeResultFromFormalTypeAndData(type, result);
            }

            if (typeof(Delegate).IsAssignableFrom(type))
            {
                var target = (Delegate)obj;
                var invocable = new Invocable(target);
                var address = ObjectServer.PublishObject(invocable);
                string result = address.Serialize();
                return composeResultFromFormalTypeAndData(type, result);
            }

            if (type.GetCustomAttributes(typeof(SerializableAttribute), false).Length > 0)
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    BinaryFormatter binaryFormatter = new BinaryFormatter();
                    binaryFormatter.Serialize(ms, obj);
                    byte[] bytes = ms.GetBuffer();
                    int length = (int)ms.Length;
                    string result = length.ToString() + ":" + Convert.ToBase64String(bytes, 0, length);
                    return composeResultFromFormalTypeAndData(type, result);
                }
            }

            if (type.IsArray)
            {
                Array array = (Array)obj;
                if (array.Rank == 1)
                {
                    Type elementType = type.GetElementType();
                    StringBuilder result = new StringBuilder();
                    for (int i = 0; i < array.Length; i++)
                    {
                        object element = array.GetValue(i);
                        Type elementFormalType = DetermineFormalTypeFromObject(element);

                        result.Append(elementFormalType.ToString());
                        result.Append(ArrayElementSeparator);
                        result.Append(Marshal(element, elementType));
                        if (i < array.Length - 1)
                        {
                            result.Append(ArrayElementSeparator);
                        }
                    }

                    return composeResultFromFormalTypeAndData(type, result.ToString());
                }
            }

            throw new ArgumentException("Cannot marshal given type.");
        }