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

标签 javascript c#

我正在尝试通过 javascript 从 C# 类库 (dll) 访问公共(public)方法,特别是用于 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 文件时是否需要执行一些设置吗?

最佳答案

这是我在 ikvm 上使用 Rhino 的示例 .net C# 应用程序。

我的示例将执行 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;

            }
        }
    }

希望对您有所帮助。

关于javascript - 如何从 javascript(rhino) 中的 C# 类库访问公共(public)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34095130/

相关文章:

c# - 无法通过 OleDbAdapter 更新 Access 数据库

C# Excel 互操作 : How to format cells to store values as text

javascript - Promise.all() 有限制吗?

javascript - AJAX:POST 后更新 div 类

javascript - 以 Ruby on Rails 形式显示/隐藏 div

c# - Service Fabric 单元测试和依赖项注入(inject)

javascript - 带有 ajax slider 扩展控件的 asp.net 控件 2 文本框

c# - 数据绑定(bind)后如何将图像设置为数据 GridView 单元格?

javascript - 在 React 中如何将值传递给状态更改回调?

javascript - getJSON 仅返回 HTMLCollection