コード例 #1
0
        private static Object AsComplex(this JsValue value, Type targetType, EngineInstance engine)
        {
            var obj        = value.FromJsValue();
            var sourceType = obj.GetType();

            if (sourceType == targetType || sourceType.GetTypeInfo().IsSubclassOf(targetType) || targetType.GetTypeInfo().IsAssignableFrom(sourceType.GetTypeInfo()))
            {
                return(obj);
            }
            else if (targetType.GetTypeInfo().IsSubclassOf(typeof(Delegate)))
            {
                var f = obj as FunctionInstance;

                if (f == null && obj is String b)
                {
                    var e = engine.Jint;
                    var p = new[] { new JsValue(b) };
                    f = new ClrFunctionInstance(e, (_this, args) => e.Eval.Call(_this, p));
                }

                if (f != null)
                {
                    return(targetType.ToDelegate(f, engine));
                }
            }

            var method = sourceType.PrepareConvert(targetType) ??
                         throw new JavaScriptException("[Internal] Could not find corresponding cast target.");

            return(method.Invoke(obj, null));
        }