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

如何在javascript(rhino)中从C#类库访问公共方法,javascript,c#,Javascript,C#,我试图从javascript的C#类库(dll)中访问公共方法,特别是用于javascript的Rhino包 以下是我想做的: namespace ReturnINT { public class ReturnINT { public static int RetornaInteiro () { try { int number = 2;

我试图从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文件时是否需要执行一些设置吗?

这是我使用Rhino over ikvm的.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; } } } 班级计划 { public static org.mozilla.javascript.Context con=null; public static org.mozilla.javascript.Scriptable scope=null; 公共静态字符串文件_PATH=@“C:\sunspider-0.9.1”; 公共静态字符串JS_Script=“load('run.JS');”; 静态void Main(字符串[]参数) { Console.WriteLine(“使用.NET Framework”+System.Environment.Version.ToString()); 字符串uuu strScriptPath=System.Configuration.ConfigurationManager.AppSettings[“脚本路径”]; if(string.IsNullOrEmpty(uuu strscript路径)=false&&System.IO.Directory.Exists(uuu strscript路径)) { System.Environment.CurrentDirectory=\uuuuuu strScriptPath; 文件路径=\uuuuu strScriptPath; } 其他的 { System.Environment.CurrentDirectory=文件路径; } string u u stroplevel=System.Configuration.ConfigurationManager.AppSettings[“opt”]; int u uu optLevel=-1; if(string.IsNullOrEmpty(\uuu\u stroplevel)==false) { if(内锥巴色(uuuuu stroplevel,外锥巴色(uuuuu optLevel)) { 如果(第9级) { ___optLevel=9; } } } string u u strScript=System.Configuration.ConfigurationManager.AppSettings[“script”]; if(string.IsNullOrEmpty(uuu_ustrScript)==false) { JS_脚本=uuu strScript; } con=org.mozilla.javascript.Context.enter(); con.setLanguageVersion(org.mozilla.javascript.Context.VERSION_ES6); //con.setGeneratingDebug(false); con.设置优化级别(optLevel); 如果(uuuu_uuuoptlevel==-1) { con.setGeneratingDebug(真); con.setGeneratingSource(true); con.setDebugger(null,null); } 其他的 { con.setGeneratingDebug(false); con.setGeneratingSource(false); con.setDebugger(null,null); } WriteLine(“启动Rhino处理OptLevel:{0}路径:{1}”,uuuuu OptLevel,System.Environment.CurrentDirectory); scope=con.initSafeStandardObjects(); org.mozilla.javascript.BaseFunction fuc=new org.mozilla.javascript.BaseFunction(); java.lang.Class css=typeof(程序); java.lang.reflect.Member loadMethod=css.getMethod(“加载”,新的java.lang.Class[]{typeof(string)}); org.mozilla.javascript.FunctionObject func=new org.mozilla.javascript.FunctionObject(“load”,loadMethod,scope); scope.put(“加载”,scope,func); java.lang.reflect.Member printMethod=css.getMethod(“print”,新的java.lang.Class[]{typeof(object)}); org.mozilla.javascript.FunctionObject funcPrint=new org.mozilla.javascript.FunctionObject(“打印”,打印方法,范围); 范围。放置(“打印”,范围,打印); java.lang.reflect.Member HogeHogeHogeMethod=css.getMethod(“hogehoge”,新的java.lang.Class[]{typeof(对象)、typeof(对象)、typeof(对象)、typeof(对象)}); DelegatableFunctionObject.deleHogehoge dele=新的DelegatableFunctionObject.deleHogehoge(hogehoge); //DelegatableFunctionObject funcHogehoge=新的DelegatableFunctionObject(“hogehoge”,hogehogeMethod,scope,dele); org.mozilla.javascript.FunctionObject funcHogehoge=new org.mozilla.javascript.FunctionObject(“hogehoge”,hogehoge方法,范围); scope.put(“hogehoge”,scope,funcHogehoge); 尝试 { con.evaluateString(作用域,JS_脚本,“”,1,null); } 捕获(例外情况除外) { Console.WriteLine(例如ToString()); } DateTime dtStart=DateTime.Now; /* org.mozilla.javascript.Script Script=con.compileString(“,”,1,null); for(int i=0;i cacheFile=new Dictionary(); 公共静态空荷载(串s) { 字符串跟踪=System.Environment.StackTrace; 字符串strScript=null; strScript=System.IO.File.ReadAllText(文件路径+“\\\”+s); //cacheFile[s]=strScript; con.evaluateString(作用域,strScript,“”,1,null); } 公共静态无效打印(对象o) { WriteLine(string.Format(“[Rhino]{0}”,o)); } 公共静态空间hogehoge(对象o、对象o2、对象o3、对象o4) { } 公共类DelegatableFunctionObject:org.mozilla.javascript.FunctionObject { 公共委托无效删除记录(对象o1、对象o2、对象o3、对象o4); private deleHogehoge uu dele=null; public DelegatableFunctionObject(字符串名称,java.lang.reflect.Member methodorContractor,org.mozilla.javascript.Scriptable作用域,deleHogehoge dele) :base(名称、方法或承包商、范围) { 这个.u dele=dele; } 公共覆盖对象调用(org.mozilla.javascr)