本文整理汇总了C#中Jint.Native.JsValue.ToObject方法的典型用法代码示例。如果您正苦于以下问题:C# JsValue.ToObject方法的具体用法?C# JsValue.ToObject怎么用?C# JsValue.ToObject使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Jint.Native.JsValue
的用法示例。
在下文中一共展示了JsValue.ToObject方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Invoke
public JsValue Invoke(MethodInfo[] methodInfos, JsValue thisObject, JsValue[] jsArguments)
{
var arguments = ProcessParamsArrays(jsArguments, methodInfos);
var methods = TypeConverter.FindBestMatch(Engine, methodInfos, arguments).ToList();
var converter = Engine.ClrTypeConverter;
foreach (var method in methods)
{
var parameters = new object[arguments.Length];
var argumentsMatch = true;
for (var i = 0; i < arguments.Length; i++)
{
var parameterType = method.GetParameters()[i].ParameterType;
if (parameterType == typeof(JsValue))
{
parameters[i] = arguments[i];
}
else
{
if (!converter.TryConvert(arguments[i].ToObject(), parameterType, CultureInfo.InvariantCulture, out parameters[i]))
{
argumentsMatch = false;
break;
}
var lambdaExpression = parameters[i] as LambdaExpression;
if (lambdaExpression != null)
{
parameters[i] = lambdaExpression.Compile();
}
}
}
if (!argumentsMatch)
{
continue;
}
// todo: cache method info
return JsValue.FromObject(Engine, method.Invoke(thisObject.ToObject(), parameters.ToArray()));
}
throw new JavaScriptException(Engine.TypeError, "No public methods with the specified arguments were found.");
}
示例2: Invoke
public JsValue Invoke(MethodInfo[] methodInfos, JsValue thisObject, JsValue[] arguments)
{
var methods = TypeConverter.FindBestMatch(Engine, methodInfos, arguments).ToList();
foreach (var method in methods)
{
var parameters = new object[arguments.Length];
try
{
for (var i = 0; i < arguments.Length; i++)
{
var parameterType = method.GetParameters()[i].ParameterType;
if (parameterType == typeof(JsValue))
{
parameters[i] = arguments[i];
}
else
{
parameters[i] = Engine.Options.GetTypeConverter().Convert(
arguments[i].ToObject(),
parameterType,
CultureInfo.InvariantCulture);
}
}
var result = JsValue.FromObject(Engine, method.Invoke(thisObject.ToObject(), parameters.ToArray()));
// todo: cache method info
return result;
}
catch
{
// ignore method
}
}
throw new JavaScriptException(Engine.TypeError, "No public methods with the specified arguments were found.");
}
示例3: ToRavenJToken
private RavenJToken ToRavenJToken(JsValue v, string propertyKey, bool recursiveCall)
{
if (v.IsBoolean())
return new RavenJValue(v.AsBoolean());
if (v.IsString())
{
const string RavenDataByteArrayToBase64 = "raven-data:byte[];base64,";
var valueAsObject = v.ToObject();
var value = valueAsObject != null ? valueAsObject.ToString() : null;
if (value != null && value.StartsWith(RavenDataByteArrayToBase64))
{
value = value.Remove(0, RavenDataByteArrayToBase64.Length);
var byteArray = Convert.FromBase64String(value);
return new RavenJValue(byteArray);
}
return new RavenJValue(value);
}
if (v.IsNumber())
{
var num = v.AsNumber();
KeyValuePair<RavenJValue, JsValue> property;
if (propertiesByValue.TryGetValue(propertyKey, out property))
{
var originalValue = property.Key;
if (originalValue.Type == JTokenType.Float ||
originalValue.Type == JTokenType.Integer)
{
// If the current value is exactly as the original value, we can return the original value before we made the JS conversion,
// which will convert a Int64 to jsFloat.
var originalJsValue = property.Value;
if (originalJsValue.IsNumber() && Math.Abs(num - originalJsValue.AsNumber()) < double.Epsilon)
return originalValue;
if (originalValue.Type == JTokenType.Integer)
return new RavenJValue((long)num);
return new RavenJValue(num);//float
}
}
// If we don't have the type, assume that if the number ending with ".0" it actually an integer.
var integer = Math.Truncate(num);
if (Math.Abs(num - integer) < double.Epsilon)
return new RavenJValue((long)integer);
return new RavenJValue(num);
}
if (v.IsNull())
return RavenJValue.Null;
if (v.IsUndefined())
return RavenJValue.Null;
if (v.IsArray())
{
var jsArray = v.AsArray();
var rja = new RavenJArray();
foreach (var property in jsArray.Properties)
{
if (InheritedProperties.Contains(property.Key))
continue;
var jsInstance = property.Value.Value;
if (!jsInstance.HasValue)
continue;
var ravenJToken = ToRavenJToken(jsInstance.Value, propertyKey + "["+property.Key +"]", recursiveCall);
if (ravenJToken == null)
continue;
rja.Add(ravenJToken);
}
return rja;
}
if (v.IsObject())
{
return ToRavenJObject(v, propertyKey, recursiveCall);
}
if (v.IsRegExp())
return null;
throw new NotSupportedException(v.Type.ToString());
}
示例4: Invoke
public JsValue Invoke(MethodInfo[] methodInfos, JsValue thisObject, JsValue[] jsArguments)
{
var arguments = ProcessParamsArrays(jsArguments, methodInfos);
var methods = TypeConverter.FindBestMatch(Engine, methodInfos, arguments).ToList();
var converter = Engine.ClrTypeConverter;
foreach (var method in methods)
{
if (method.IsGenericMethodDefinition)
{
Type[] typeArguments = new Type[arguments.Length];
for (int i = 0; i < arguments.Length; i++)
{
typeArguments[i] = arguments[i].ToObject() as Type;
}
var genericMethod = (method as MethodInfo).MakeGenericMethod(typeArguments);
return JsValue.FromObject(Engine, genericMethod);
}
var parameters = new object[arguments.Length];
var argumentsMatch = true;
for (var i = 0; i < arguments.Length; i++)
{
var parameterType = method.GetParameters()[i].ParameterType;
if (parameterType == typeof(JsValue))
{
parameters[i] = arguments[i];
}
else if (parameterType == typeof(JsValue[]) && arguments[i].IsArray())
{
// Handle specific case of F(params JsValue[])
var arrayInstance = arguments[i].AsArray();
var len = TypeConverter.ToInt32(arrayInstance.Get("length"));
var result = new JsValue[len];
for (var k = 0; k < len; k++)
{
var pk = k.ToString();
result[k] = arrayInstance.HasProperty(pk)
? arrayInstance.Get(pk)
: JsValue.Undefined;
}
parameters[i] = result;
}
else
{
if (!converter.TryConvert(arguments[i].ToObject(), parameterType, CultureInfo.InvariantCulture, out parameters[i]))
{
argumentsMatch = false;
break;
}
var lambdaExpression = parameters[i] as LambdaExpression;
if (lambdaExpression != null)
{
parameters[i] = lambdaExpression.Compile();
}
}
}
if (!argumentsMatch)
{
continue;
}
// todo: cache method info
return JsValue.FromObject(Engine, method.Invoke(method.IsStatic ? null : thisObject.ToObject(), parameters.ToArray()));
}
throw new JavaScriptException(Engine.TypeError, "No public methods with the specified arguments were found.");
}
示例5: ToObject
public static Object ToObject(JsValue arg)
{
return arg.ToObject();
}