本文整理汇总了C#中JsValue.TryCast方法的典型用法代码示例。如果您正苦于以下问题:C# JsValue.TryCast方法的具体用法?C# JsValue.TryCast怎么用?C# JsValue.TryCast使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类JsValue
的用法示例。
在下文中一共展示了JsValue.TryCast方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ToString
public JsValue ToString(JsValue thisObject, JsValue[] arguments)
{
var o = thisObject.TryCast<ObjectInstance>();
if (o == null)
{
throw new JavaScriptException(Engine.TypeError);
}
var name = TypeConverter.ToString(o.Get("name"));
var msgProp = o.Get("message");
string msg;
if (msgProp == Undefined.Instance)
{
msg = "";
}
else
{
msg = TypeConverter.ToString(msgProp);
}
if (name == "")
{
return msg;
}
if (msg == "")
{
return name;
}
return name + ": " + msg;
}
示例2: Bind
private JsValue Bind(JsValue thisObj, JsValue[] arguments)
{
var target = thisObj.TryCast<ICallable>(x =>
{
throw new JavaScriptException(Engine.TypeError);
});
var thisArg = arguments.At(0);
var f = new BindFunctionInstance(Engine) {Extensible = true};
f.TargetFunction = thisObj;
f.BoundThis = thisArg;
f.BoundArgs = arguments.Skip(1).ToArray();
f.Prototype = Engine.Function.PrototypeObject;
var o = target as FunctionInstance;
if (o != null)
{
var l = TypeConverter.ToNumber(o.Get("length")) - (arguments.Length - 1);
f.FastAddProperty("length", System.Math.Max(l, 0), false, false, false);
}
else
{
f.FastAddProperty("length", 0, false, false, false);
}
var thrower = Engine.Function.ThrowTypeError;
f.DefineOwnProperty("caller", new PropertyDescriptor(thrower, thrower, false, false), false);
f.DefineOwnProperty("arguments", new PropertyDescriptor(thrower, thrower, false, false), false);
return f;
}
示例3: HasInstance
public virtual bool HasInstance(JsValue v)
{
var vObj = v.TryCast<ObjectInstance>();
if (vObj == null)
{
return false;
}
var po = Get("prototype");
if (!po.IsObject())
{
throw new JavaScriptException(_engine.TypeError, string.Format("Function has non-object prototype '{0}' in instanceof check", TypeConverter.ToString(po)));
}
var o = po.AsObject();
if (o == null)
{
throw new JavaScriptException(_engine.TypeError);
}
while (true)
{
vObj = vObj.Prototype;
if (vObj == null)
{
return false;
}
if (vObj == o)
{
return true;
}
}
}
示例4: ToRegExpString
private JsValue ToRegExpString(JsValue thisObj, JsValue[] arguments)
{
var regExp = thisObj.TryCast<RegExpInstance>();
return "/" + regExp.Source + "/"
+ (regExp.Flags.Contains("g") ? "g" : "")
+ (regExp.Flags.Contains("i") ? "i" : "")
+ (regExp.Flags.Contains("m") ? "m" : "")
;
}
示例5: ValueOf
private JsValue ValueOf(JsValue thisObj, JsValue[] arguments)
{
var number = thisObj.TryCast<NumberInstance>();
if (number == null)
{
throw new JavaScriptException(Engine.TypeError);
}
return number.PrimitiveValue;
}
示例6: ToString
private JsValue ToString(JsValue thisObj, JsValue[] arguments)
{
var func = thisObj.TryCast<FunctionInstance>();
if (func == null)
{
throw new JavaScriptException(Engine.TypeError, "Function object expected.");
}
return System.String.Format("function() {{ ... }}");
}
示例7: ToLocaleString
private JsValue ToLocaleString(JsValue thisObject, JsValue[] arguments)
{
if (!thisObject.IsNumber() && (thisObject.TryCast<NumberInstance>() == null))
{
throw new JavaScriptException(Engine.TypeError);
}
var m = TypeConverter.ToNumber(thisObject);
if (double.IsNaN(m))
{
return "NaN";
}
if (m.Equals(0))
{
return "0";
}
if (m < 0)
{
return "-" + ToLocaleString(-m, arguments);
}
if (double.IsPositiveInfinity(m) || m >= double.MaxValue)
{
return "Infinity";
}
if (double.IsNegativeInfinity(m) || m <= -double.MaxValue)
{
return "-Infinity";
}
return m.ToString("n", Engine.Options.GetCulture());
}
示例8: GetUTCMilliseconds
private static JsValue GetUTCMilliseconds(JsValue thisObj, JsValue[] arguments)
{
var t = thisObj.TryCast<DateInstance>().PrimitiveValue;
if (double.IsNaN(t))
{
return double.NaN;
}
return MsFromTime(t);
}
示例9: GetUTCDay
private static JsValue GetUTCDay(JsValue thisObj, JsValue[] arguments)
{
var t = thisObj.TryCast<DateInstance>().PrimitiveValue;
if (double.IsNaN(t))
{
return double.NaN;
}
return WeekDay(t);
}
示例10: GetTime
private static JsValue GetTime(JsValue thisObj, JsValue[] arguments)
{
if (double.IsNaN(thisObj.TryCast<DateInstance>().PrimitiveValue))
{
return double.NaN;
}
return thisObj.TryCast<DateInstance>().PrimitiveValue;
}
示例11: ToLocaleTimeString
private JsValue ToLocaleTimeString(JsValue thisObj, JsValue[] arguments)
{
return thisObj.TryCast<DateInstance>().ToDateTime().ToString("T");
}
示例12: ValueOf
private JsValue ValueOf(JsValue thisObj, JsValue[] arguments)
{
var s = thisObj.TryCast<StringInstance>();
if (s == null)
{
throw new JavaScriptException(Engine.TypeError);
}
return s.PrimitiveValue;
}
示例13: ToTimeString
private static JsValue ToTimeString(JsValue thisObj, JsValue[] arguments)
{
return thisObj.TryCast<DateInstance>().ToDateTime().ToString("T", CultureInfo.InvariantCulture);
}
示例14: ValueOf
private static JsValue ValueOf(JsValue thisObj, JsValue[] arguments)
{
return thisObj.TryCast<DateInstance>().PrimitiveValue;
}
示例15: GetMonth
private static JsValue GetMonth(JsValue thisObj, JsValue[] arguments)
{
var t = thisObj.TryCast<DateInstance>().PrimitiveValue;
if (double.IsNaN(t))
{
return double.NaN;
}
return MonthFromTime(LocalTime(t));
}