如何在javascript(rhino)中从C#类库访问公共方法

时间:2015-12-04 18:32:11

标签: javascript c#

我正在尝试从javascript访问C#类库(dll)中的公共方法,特别是javascript的Rhino包。

以下是我要做的事情:

namespace ReturnINT
{
    public class ReturnINT
    {

        public static int RetornaInteiro ()
        {
            try
            {
                int number = 2;

                return number;
            }
            catch (Exception)
            {
                return 1;
            }
        }
    }
}

我从上面的代码创建了一个类库,或者确切地说是一个dll文件。现在我想在我的javascript中访问这个dll,但我不想创建任何包装器,这在其他一些线程中都有提及。

有人可以在这里指导我,如果我需要在创建dll文件时执行某些设置,请告诉我吗?

1 个答案:

答案 0 :(得分:0)

这是我使用Rhino over ikvm的示例.net C#app。

我的示例将执行sunspider基准脚本。

class Program
    {
        public static org.mozilla.javascript.Context con = null;
        public static org.mozilla.javascript.Scriptable scope = null;
        public static string FILE_PATH = @"C:\sunspider-0.9.1";


        public static string JS_Script = "load('run.js');";
        static void Main(string[] args)
        {
            Console.WriteLine("Using .NET Framework " + System.Environment.Version.ToString());
            string ___strScriptPath = System.Configuration.ConfigurationManager.AppSettings["script_path"];
            if (string.IsNullOrEmpty(___strScriptPath) == false && System.IO.Directory.Exists(___strScriptPath))
            {
                System.Environment.CurrentDirectory = ___strScriptPath;
                FILE_PATH = ___strScriptPath;
            }
            else
            {
                System.Environment.CurrentDirectory = FILE_PATH;
            }
            string ___strOptLevel = System.Configuration.ConfigurationManager.AppSettings["opt"];
            int ___optLevel = -1;
            if (string.IsNullOrEmpty(___strOptLevel) == false)
            {
                if (int.TryParse(___strOptLevel, out ___optLevel))
                {
                    if (___optLevel  9)
                    {
                        ___optLevel = 9;
                    }
                }
            }
            string ___strScript = System.Configuration.ConfigurationManager.AppSettings["script"];

            if (string.IsNullOrEmpty(___strScript) == false)
            {
                JS_Script = ___strScript;
            }
            con = org.mozilla.javascript.Context.enter();
            con.setLanguageVersion(org.mozilla.javascript.Context.VERSION_ES6);
            //con.setGeneratingDebug(false);

            con.setOptimizationLevel(___optLevel);
            if (___optLevel == -1)
            {
                con.setGeneratingDebug(true);
                con.setGeneratingSource(true);
                con.setDebugger(null, null);
            }
            else
            {
                con.setGeneratingDebug(false);
                con.setGeneratingSource(false);
                con.setDebugger(null, null);
            }
            Console.WriteLine("Starting Rhino Processing OptLevel : {0} Path : {1}", ___optLevel, System.Environment.CurrentDirectory);
            scope = con.initSafeStandardObjects();
            org.mozilla.javascript.BaseFunction fuc = new org.mozilla.javascript.BaseFunction();
            java.lang.Class css = typeof(Program);
            java.lang.reflect.Member   loadMethod = css.getMethod("load", new java.lang.Class[]{typeof(string)});
            org.mozilla.javascript.FunctionObject func = new org.mozilla.javascript.FunctionObject("load", loadMethod, scope);
            scope.put("load", scope,func);

            java.lang.reflect.Member printMethod = css.getMethod("print", new java.lang.Class[] { typeof(object) });
            org.mozilla.javascript.FunctionObject funcPrint = new org.mozilla.javascript.FunctionObject("print", printMethod, scope);
            scope.put("print", scope, funcPrint);

            java.lang.reflect.Member hogehogeMethod = css.getMethod("hogehoge", new java.lang.Class[] { typeof(object), typeof(object), typeof(object), typeof(object) });
            DelegatableFunctionObject.deleHogehoge dele = new DelegatableFunctionObject.deleHogehoge(hogehoge);
            //DelegatableFunctionObject funcHogehoge = new DelegatableFunctionObject("hogehoge", hogehogeMethod, scope,dele);
            org.mozilla.javascript.FunctionObject funcHogehoge = new org.mozilla.javascript.FunctionObject("hogehoge", hogehogeMethod, scope);
            scope.put("hogehoge", scope, funcHogehoge);
            try
            {
                con.evaluateString(scope, JS_Script, "", 1, null);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            DateTime dtStart = DateTime.Now;
            /*
            org.mozilla.javascript.Script script = con.compileString("", "", 1, null);
            for (int i = 0; i  cacheFile = new Dictionary();
        public static void load(string s)
        {
            string trace = System.Environment.StackTrace;
            string strScript = null;

                strScript = System.IO.File.ReadAllText(FILE_PATH + "\\" + s);
             //   cacheFile[s] = strScript;

           con.evaluateString(scope, strScript, "", 1, null);
        }
        public static void print(object o)
        {
            Console.WriteLine(string.Format("[Rhino] {0}", o));
        }
        public static void hogehoge(object o, object o2, object o3, object o4)
        {

        }
        public class DelegatableFunctionObject : org.mozilla.javascript.FunctionObject
        {
            public delegate void deleHogehoge(object o1, object o2, object o3, object o4);
            private deleHogehoge __dele = null;
            public DelegatableFunctionObject(string name, java.lang.reflect.Member methodOrContructior, org.mozilla.javascript.Scriptable scope,deleHogehoge dele)
                : base(name, methodOrContructior, scope)
            {
                this.__dele = dele;
            }
            public override object call(org.mozilla.javascript.Context cx, org.mozilla.javascript.Scriptable scope, org.mozilla.javascript.Scriptable thisObj, object[] args)
            {
                if (__dele != null)
                {
                    switch (args.Length)
                    {
                        case 1:
                            __dele.Invoke(args[0], null, null, null);
                            break;
                    }
                }
                return null;

            }
        }
    }

我希望这会对你有所帮助。