本文整理汇总了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);
});
}
示例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);
});
}
示例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);
});
}
示例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);
});
}
示例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));
});
}
示例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);
});
}