当前位置: 首页>>代码示例>>C#>>正文


C# BoxedValue.Unbox方法代码示例

本文整理汇总了C#中BoxedValue.Unbox方法的典型用法代码示例。如果您正苦于以下问题:C# BoxedValue.Unbox方法的具体用法?C# BoxedValue.Unbox怎么用?C# BoxedValue.Unbox使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在BoxedValue的用法示例。


在下文中一共展示了BoxedValue.Unbox方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: AppendJson

        /// <summary>
        /// Opens a file, appends the specified string to the file, and then closes the file. If the file does not exist, this method creates a file, writes the specified string to the file, then closes the file.
        /// </summary>
        internal static void AppendJson(FunctionObject ctx, ScriptObject instance, BoxedValue path, BoxedValue contents, BoxedValue onComplete, BoxedValue encodingName)
        {
            if (!path.IsString)
                throw new ArgumentException("[appendJson] First parameter should be defined and be a string.");
            if (!contents.IsStrictlyObject)
                throw new ArgumentException("[appendJson] Second parameter should be defined and be an object.");
            if (!onComplete.IsUndefined && !onComplete.IsFunction)
                throw new ArgumentException("[appendJson] Third parameter should be an onComplete function.");

            // Get the curent channel
            var channel = Channel.Current;

            // Dispatch the task
            channel.Async(() =>
            {
                // The encoding to use
                Encoding encoding = encodingName.IsString
                    ? TextEncoding.GetEncoding(encodingName.String)
                    : null;

                // Defaults to UTF8
                if (encoding == null)
                    encoding = TextEncoding.UTF8;

                // Unbox the array of lines and execute the append
                File.AppendAllText(
                    path.Unbox<string>(),
                    Native.Serialize(instance.Env, contents, false).Unbox<string>(),
                    encoding
                    );

                // Dispatch the on complete asynchronously
                channel.DispatchCallback(onComplete, instance);
            });
        }
开发者ID:mizzunet,项目名称:spike-box,代码行数:38,代码来源:FileObject.cs

示例2: WriteLines

        /// <summary>
        /// Creates a new file by using the specified encoding, writes a collection of strings to the file, and then closes the file.
        /// </summary>
        internal static void WriteLines(FunctionObject ctx, ScriptObject instance, BoxedValue path, BoxedValue contents, BoxedValue onComplete, BoxedValue encodingName)
        {
            if (!path.IsString)
                throw new ArgumentException("[writeLines] TFirst parameter should be defined and be a string.");
            if (!contents.IsArray)
                throw new ArgumentException("[writeLines] TSecond parameter should be defined and be an array.");
            if (!onComplete.IsUndefined && !onComplete.IsFunction)
                throw new ArgumentException("[writeLines] Third parameter should be an onComplete function.");

            // Get the curent channel
            var channel = Channel.Current;

            // Dispatch the task
            channel.Async(() =>
            {
                // The encoding to use
                Encoding encoding = encodingName.IsString
                    ? TextEncoding.GetEncoding(encodingName.String)
                    : null;

                // Defaults to UTF8
                if (encoding == null)
                    encoding = TextEncoding.UTF8;

                // Unbox the array of lines and execute the append
                File.WriteAllLines(
                    path.Unbox<string>(),
                    contents.Array.ToArray<string>(),
                    encoding
                    );

                // Dispatch the on complete asynchronously
                channel.DispatchCallback(onComplete, instance);
            });
        }
开发者ID:mizzunet,项目名称:spike-box,代码行数:38,代码来源:FileObject.cs

示例3: WriteBuffer

        /// <summary>
        /// Creates a new file, writes the specified byte array to the file, and then closes the file. If the target file already exists, it is overwritten.
        /// </summary>
        internal static void WriteBuffer(FunctionObject ctx, ScriptObject instance, BoxedValue path, BoxedValue contents, BoxedValue onComplete)
        {
            if (!path.IsString)
                throw new ArgumentException("[writeBuffer] First parameter should be defined and be a string.");
            if (!contents.IsObject || !(contents.Object is BufferObject))
                throw new ArgumentException("[writeBuffer] Second parameter should be defined and be a Buffer.");
            if (!onComplete.IsUndefined && !onComplete.IsFunction)
                throw new ArgumentException("[writeBuffer] Third parameter should be an onComplete function.");

            // Get the curent channel
            var channel = Channel.Current;

            // Dispatch the task
            channel.Async(() =>
            {
                // Get the buffer
                var buffer = contents.Object as BufferObject;

                // Write the contents
                File.WriteAllBytes(
                    path.Unbox<string>(),
                    buffer.Array
                    );

                // Dispatch the on complete asynchronously
                channel.DispatchCallback(onComplete, instance);
            });
        }
开发者ID:mizzunet,项目名称:spike-box,代码行数:31,代码来源:FileObject.cs

示例4: ReadText

        /// <summary>
        /// Opens a file, reads all lines of the file with the specified encoding, and then closes the file.
        /// </summary>
        internal static void ReadText(FunctionObject ctx, ScriptObject instance, BoxedValue path, BoxedValue onComplete, BoxedValue encodingName)
        {
            if (!path.IsString)
                throw new ArgumentException("[readText] First parameter should be defined and be a string.");
            if (!onComplete.IsFunction)
                throw new ArgumentException("[readText] Second parameter should be an onComplete function.");

            // Get the curent channel
            var channel = Channel.Current;

            // Dispatch the task
            channel.Async(() =>
            {
                // The encoding to use
                Encoding encoding = encodingName.IsString
                    ? TextEncoding.GetEncoding(encodingName.String)
                    : null;

                // Defaults to UTF8
                if (encoding == null)
                    encoding = TextEncoding.UTF8;

                // Read the text
                var text = BoxedValue.Box(
                        File.ReadAllText(path.Unbox<string>(), encoding)
                    );

                // Dispatch the on complete asynchronously
                channel.DispatchCallback(onComplete, instance, text);
            });
        }
开发者ID:mizzunet,项目名称:spike-box,代码行数:34,代码来源:FileObject.cs

示例5: ReadLines

        /// <summary>
        /// Opens a file, reads all lines of the file with the specified encoding, and then closes the file.
        /// </summary>
        internal static void ReadLines(FunctionObject ctx, ScriptObject instance, BoxedValue path, BoxedValue onComplete, BoxedValue encodingName)
        {
            if (!path.IsString)
                throw new ArgumentException("[readLines] First parameter should be defined and be a string.");
            if (!onComplete.IsFunction)
                throw new ArgumentException("[readLines] Second parameter should be an onComplete function.");

            // Get the curent channel
            var channel = Channel.Current;

            // Dispatch the task
            channel.Async(() =>
            {
                // The encoding to use
                Encoding encoding = encodingName.IsString
                    ? TextEncoding.GetEncoding(encodingName.String)
                    : null;

                // Defaults to UTF8
                if (encoding == null)
                    encoding = TextEncoding.UTF8;

                // Unbox the array of lines and execute the append
                var lines = File.ReadAllLines(
                    path.Unbox<string>(),
                    encoding
                    );

                // Create a new array
                var array = new ArrayObject(instance.Env, (uint)lines.Length);
                for (uint i = 0; i < lines.Length; ++i)
                {
                    // Put a boxed string inside for each line
                    array.Put(i, BoxedValue.Box(lines[i]));
                }

                // Dispatch the on complete asynchronously
                channel.DispatchCallback(onComplete, instance, BoxedValue.Box(array));

            });
        }
开发者ID:mizzunet,项目名称:spike-box,代码行数:44,代码来源:FileObject.cs

示例6: ReadBuffer

        /// <summary>
        /// Opens a binary file, reads the contents of the file into a byte array, and then closes the file.
        /// </summary>
        internal static void ReadBuffer(FunctionObject ctx, ScriptObject instance, BoxedValue path, BoxedValue onComplete)
        {
            if (!path.IsString)
                throw new ArgumentException("[readBuffer] First parameter should be defined and be a string.");
            if (!onComplete.IsFunction)
                throw new ArgumentException("[readBuffer] Second parameter should be an onComplete function.");

            // Get the curent channel
            var channel = Channel.Current;

            // Dispatch the task
            channel.Async(() =>
            {
                // Read the file
                var arr = File.ReadAllBytes(path.Unbox<string>());
                var seg = new ArraySegment<byte>(arr);

                // Unbox the array of lines and execute the append
                var buffer = BoxedValue.Box(
                    new BufferObject(seg, instance.Env)
                    );

                // Dispatch the on complete asynchronously
                channel.DispatchCallback(onComplete, instance, buffer);
            });
        }
开发者ID:mizzunet,项目名称:spike-box,代码行数:29,代码来源:FileObject.cs


注:本文中的BoxedValue.Unbox方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。