Ejemplo n.º 1
0
 public static DateTime DateTimeGet(dynamic dynamicOBJ, string propertyname)
 {
     try
     {
         var value = DynamicExtensions.GetProperty(dynamicOBJ, propertyname);
         if (value == null)
         {
             return(default);
Ejemplo n.º 2
0
 public static bool BoolGet(dynamic dynamicOBJ, string propertyname)
 {
     try
     {
         var value = DynamicExtensions.GetProperty(dynamicOBJ, propertyname);
         return(Convert.ToBoolean(value));
     }
     catch (Exception ex)
     {
         Console.WriteLine(string.Format("异常[{0}]:{1}", System.Reflection.MethodBase.GetCurrentMethod().ReflectedType.FullName, ex.Message));
     }
     finally
     {
     }
     return(false);
 }
Ejemplo n.º 3
0
    public static string StringGet(dynamic dynamicOBJ, string propertyname)
    {
        try
        {
            var value = DynamicExtensions.GetProperty(dynamicOBJ, propertyname);
            if (value == null)
            {
                return("");
            }

            return((string)value);
        }
        catch (Exception ex)
        {
            Console.WriteLine(string.Format("异常[{0}]:{1}", System.Reflection.MethodBase.GetCurrentMethod().ReflectedType.FullName, ex.Message));
        }
        finally
        {
        }
        return("");
    }
Ejemplo n.º 4
0
        /// <summary>
        /// Called via a javascript to require and return the requested package.
        /// </summary>
        /// <param name="packageId">ID of the RequirePackage to require.</param>
        /// <param name="scriptUri">A script uri.  This is only needed if the packageId doesn't meet the script convention name and the package is not a registered package.</param>
        /// <returns>The return object to use for the require. Either the export from the require script or the returned HostObject if not script is present.</returns>
        public object Require(string packageId, string scriptUri)
        {
            RequiredPackage package;
            bool            hasUri         = !String.IsNullOrEmpty(scriptUri);
            bool            packageCreated = false;

            if (packageId.Contains("/") || packageId.Contains("\\"))
            {
                if (!hasUri)
                {
                    scriptUri = packageId;
                    hasUri    = true;
                }

                packageId = DerivePackageIdFromUri(scriptUri);

                if (packageId.Length == 0)
                {
                    throw new ArgumentException(
                              "The provided packageId is not a valid package name. The packageId must be a valid file path or uri if path characters are contained in the name.");
                }
            }

            if (!RequireManager.TryGetPackage(packageId, out package))
            {
                if (!hasUri)
                {
                    throw new KeyNotFoundException(String.Format("The package with ID {0} was not found, did you register this package?", packageId));
                }

                package = new RequiredPackage {
                    PackageId = packageId, ScriptUri = scriptUri
                };
                packageCreated = true;
            }

            var options = new ExecutionOptions
            {
                HostObjects = package.HostObjects,
                HostTypes   = package.HostTypes
            };

            Engine.ApplyOptions(options);

            if (!String.IsNullOrEmpty(package.ScriptUri))
            {
                var compiledScript = Compiler.Compile(new IncludeScript
                {
                    Uri             = package.ScriptUri,
                    PrependCode     = "var " + packageId + " = {};",
                    RequiredPackage = package
                });

                Engine.Execute(compiledScript);

                var outputObject = DynamicExtensions.GetProperty(Engine.Script, packageId);

                if (outputObject is Undefined)
                {
                    //try to find pascal case if camel is not present.
                    outputObject = DynamicExtensions.GetProperty(Engine.Script, packageId.ToPascalCase());
                }

                if (packageCreated)
                {
                    RequireManager.RegisterPackage(package);
                }

                return(outputObject.exports);
            }

            if (options.HostObjects.SafeAny())
            {
                return(options.HostObjects[0]);
            }

            return(null);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Called via a javascript to require and return the requested package.
        /// </summary>
        /// <param name="packageId">ID of the RequirePackage to require.</param>
        /// <param name="scriptUri">A script uri.  This is only needed if the packageId doesn't meet the script convention name and the package is not a registered package.</param>
        /// <returns>The return object to use for the require. Either the export from the require script or the returned HostObject if not script is present.</returns>
        public object Require(string packageId, string scriptUri)
        {
            RequiredPackage package;
            bool            hasUri         = !String.IsNullOrEmpty(scriptUri);
            bool            packageCreated = false;

            if (packageId.StartsWith("/"))
            {
                packageId = "." + packageId;
            }
            if (packageId.Contains("/") || packageId.Contains("\\"))
            {
                //http 和 file 类型的都会进来
                if (!hasUri)
                {
                    scriptUri = packageId;
                    hasUri    = true;
                }

                //拿到真正的地址
                var real = GetRealFilePath(scriptUri);

                packageId = real.PackageId;
                scriptUri = real.NativeRequirePath;


                if (packageId.Length == 0)
                {
                    throw new ArgumentException(
                              "The provided packageId is not a valid package name. The packageId must be a valid file path or uri if path characters are contained in the name.");
                }
            }

            if (!RequireManager.TryGetPackage(packageId, out package))
            {
                if (!hasUri)
                {
                    throw new KeyNotFoundException(String.Format("The package with ID {0} was not found, did you register this package?", packageId));
                }

                package = new RequiredPackage {
                    PackageId = packageId, ScriptUri = scriptUri
                };
                packageCreated = true;
            }

            if (package.Exports != null)
            {
                return(package.Exports);
            }

            var options = new ExecutionOptions
            {
                HostObjects = package.HostObjects,
                HostTypes   = package.HostTypes
            };

            Engine.ApplyOptions(options);

            if (!String.IsNullOrEmpty(package.ScriptUri))
            {
                var includScript = new IncludeScript
                {
                    Uri             = package.ScriptUri,
                    PrependCode     = "var " + package.PackageId + " = {};",
                    RequiredPackage = package
                };

                var compiledScript = Compiler.Compile(includScript);

                if (compiledScript == null && includScript.Exports != null)//DLL场景
                {
                    if (packageCreated)
                    {
                        package.Exports = includScript.Exports;
                        RequireManager.RegisterPackage(package);
                    }
                    _LastRequireDic.TryRemove(_lastRequireIdex, out string _aa);
                    _lastRequireIdex--;
                    return(package.Exports);
                }

                Engine.Execute(compiledScript);

                var outputObject = DynamicExtensions.GetProperty(Engine.Script, package.PackageId);

                if (outputObject is Undefined)
                {
                    //try to find pascal case if camel is not present.
                    outputObject = DynamicExtensions.GetProperty(Engine.Script, package.PackageId.ToPascalCase());
                }

                if (packageCreated)
                {
                    RequireManager.RegisterPackage(package);
                }
                if (_lastRequireIdex > 0)
                {
                    _LastRequireDic.TryRemove(_lastRequireIdex, out string _a);
                    _lastRequireIdex--;
                }

                return(outputObject.exports);
            }

            if (options.HostObjects.SafeAny())
            {
                return(options.HostObjects[0].Target);
            }

            return(null);
        }