JavaScriptEngineSwitcher.ChakraCore.JsRt.JsValue.ToString C# (CSharp) Method

ToString() public method

Retrieves a string pointer of a String value

This function retrieves the string pointer of a String value. It will fail with InvalidArgument if the type of the value is not String.

Requires an active script context.

public ToString ( ) : string
return string
		public new string ToString()
		{
			string result;
			JsErrorCode errorCode;

			if (Utils.IsWindows())
			{
				IntPtr ptr;
				UIntPtr stringLength;

				errorCode = NativeMethods.JsStringToPointer(this, out ptr, out stringLength);
				JsErrorHelpers.ThrowIfError(errorCode);

				result = Marshal.PtrToStringUni(ptr, (int)stringLength);
			}
			else
			{
				byte[] buffer = null;
				UIntPtr bufferSize = UIntPtr.Zero;
				UIntPtr written;

				errorCode = NativeMethods.JsCopyStringUtf8(this, buffer, bufferSize, out written);
				JsErrorHelpers.ThrowIfError(errorCode);

				buffer = new byte[(int)written];
				bufferSize = new UIntPtr((uint)written);

				errorCode = NativeMethods.JsCopyStringUtf8(this, buffer, bufferSize, out written);
				JsErrorHelpers.ThrowIfError(errorCode);

				result = Encoding.GetEncoding(0).GetString(buffer);
			}

			return result;
		}

Usage Example

コード例 #1
0
        /// <summary>
        /// Makes a mapping of value from the script type to a host type
        /// </summary>
        /// <param name="value">The source value</param>
        /// <returns>The mapped value</returns>
        public object MapToHostType(JsValue value)
        {
            JsValueType valueType = value.ValueType;
            object      result    = null;

            switch (valueType)
            {
            case JsValueType.Null:
                result = null;
                break;

            case JsValueType.Undefined:
                result = Undefined.Value;
                break;

            case JsValueType.Boolean:
                result = value.ToBoolean();
                break;

            case JsValueType.Number:
                result = NumericHelpers.CastDoubleValueToCorrectType(value.ToDouble());
                break;

            case JsValueType.String:
                result = value.ToString();
                break;

            case JsValueType.Function:
                JsPropertyId externalObjectPropertyId = JsPropertyId.FromString(ExternalObjectPropertyName);
                if (value.HasProperty(externalObjectPropertyId))
                {
                    JsValue externalObjectValue = value.GetProperty(externalObjectPropertyId);
                    result = externalObjectValue.HasExternalData ?
                             GCHandle.FromIntPtr(externalObjectValue.ExternalData).Target : null;
                }

                result = result ?? value.ConvertToObject();
                break;

            case JsValueType.Object:
            case JsValueType.Error:
            case JsValueType.Array:
            case JsValueType.Symbol:
            case JsValueType.ArrayBuffer:
            case JsValueType.TypedArray:
            case JsValueType.DataView:
                result = value.HasExternalData ?
                         GCHandle.FromIntPtr(value.ExternalData).Target : value.ConvertToObject();
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            return(result);
        }