Example #1
0
        /// <summary>
        ///     Converts the value to <c>String</c> using regular JavaScript semantics.
        /// </summary>
        /// <remarks>
        ///     Requires an active script context.
        /// </remarks>
        /// <returns>The converted value.</returns>
        public JavaScriptValue ConvertToString()
        {
            JavaScriptValue stringReference;

            Native.ThrowIfError(Native.JsConvertValueToString(this, out stringReference));
            return(stringReference);
        }
        public string ConvertValueToString(JavaScriptValue value)
        {
            string result = null;

            try
            {
                // Convert your script result to String in JavaScript; redundant if your script returns a String
                JavaScriptValue resultJSString;

                JavaScriptErrorCode errorCode = Native.JsConvertValueToString(value, out resultJSString);

                if (errorCode != JavaScriptErrorCode.NoError)
                {
                    string errorMessage = string.Format("Failed to convert value to JsString with error [{0}]", errorCode);

                    System.Diagnostics.Debug.WriteLine(errorMessage);

                    throw new Exception(errorMessage);
                }

                // Project script result in JS back to C#.
                IntPtr  resultPtr;
                UIntPtr stringLength;

                errorCode = Native.JsStringToPointer(resultJSString, out resultPtr, out stringLength);

                if (errorCode != JavaScriptErrorCode.NoError)
                {
                    string errorMessage = string.Format("Failed to convert JsString to Pointer with error [{0}]", errorCode);

                    System.Diagnostics.Debug.WriteLine(errorMessage);

                    throw new Exception(errorMessage);
                }

                result = Marshal.PtrToStringUni(resultPtr);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("An exception occurred while converting JavaScriptValue with error [{0}]", ex.Message);

                throw;
            }

            return(result);
        }